textarea-markdown
Advanced tools
Comparing version 1.3.3 to 1.3.4
{ | ||
"name": "textarea-markdown", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "Make textarea a markdown editor.", | ||
@@ -28,3 +28,6 @@ "main": "lib/textarea-markdown.js", | ||
"babel-preset-es2015": "^6.24.1", | ||
"eslint": "^8.35.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"import": "0.0.6", | ||
"prettier": "^2.8.4", | ||
"webpack": "^5.74.0", | ||
@@ -31,0 +34,0 @@ "webpack-dev-server": "^4.11.1" |
@@ -1,5 +0,5 @@ | ||
import 'whatwg-fetch' | ||
import MarkdownIt from 'markdown-it' | ||
import { filetypeextension } from 'magic-bytes.js' | ||
import { filesize } from "filesize" | ||
import "whatwg-fetch"; | ||
import MarkdownIt from "markdown-it"; | ||
import { filetypeextension } from "magic-bytes.js"; | ||
import { filesize } from "filesize"; | ||
@@ -9,34 +9,40 @@ export default class TextareaMarkdown { | ||
this.textarea = textarea; | ||
this.options = Object.assign({ | ||
useUploader: true, | ||
endPoint: '/api/image.json', | ||
paramName: 'file', | ||
responseKey: 'url', | ||
csrfToken: null, | ||
placeholder: 'uploading %filename ...', | ||
imageableExtensions: ['jpeg', 'png', 'gif'], | ||
afterPreview: () => {}, | ||
plugins: [], | ||
markdownOptions: Object.assign({ | ||
html: true, | ||
breaks: true, | ||
langPrefix: 'language-', | ||
linkify: true | ||
}) | ||
}, options) | ||
this.options = Object.assign( | ||
{ | ||
useUploader: true, | ||
endPoint: "/api/image.json", | ||
paramName: "file", | ||
responseKey: "url", | ||
csrfToken: null, | ||
placeholder: "uploading %filename ...", | ||
imageableExtensions: ["jpeg", "png", "gif"], | ||
videoExtensions: ["mov", "mp4", "webm"], | ||
afterPreview: () => {}, | ||
plugins: [], | ||
markdownOptions: Object.assign({ | ||
html: true, | ||
breaks: true, | ||
langPrefix: "language-", | ||
linkify: true, | ||
}), | ||
}, | ||
options | ||
); | ||
this.previews = []; | ||
this.setPreview(); | ||
this.applyPreview(); | ||
if(this.options.useUploader) { | ||
textarea.addEventListener("dragover", e => e.preventDefault()); | ||
textarea.addEventListener("drop", e => this.drop(e)); | ||
if (this.options.useUploader) { | ||
textarea.addEventListener("dragover", (e) => e.preventDefault()); | ||
textarea.addEventListener("drop", (e) => this.drop(e)); | ||
} | ||
textarea.addEventListener("paste", e => this.paste(e)); | ||
textarea.addEventListener("keyup", e => this.keyup(e)); | ||
textarea.addEventListener("paste", (e) => this.paste(e)); | ||
textarea.addEventListener("keyup", (e) => this.keyup(e)); | ||
} | ||
setPreview() { | ||
const selector = this.textarea.getAttribute('data-preview'); | ||
const selector = this.textarea.getAttribute("data-preview"); | ||
if (selector) { | ||
Array.from(document.querySelectorAll(selector), e => this.previews.push(e)) | ||
Array.from(document.querySelectorAll(selector), (e) => | ||
this.previews.push(e) | ||
); | ||
} | ||
@@ -58,3 +64,3 @@ } | ||
keyup(event) { | ||
keyup() { | ||
this.applyPreview(); | ||
@@ -71,4 +77,4 @@ } | ||
// IE | ||
var evt = document.createEventObject(); | ||
return element.fireEvent("on" + event, evt) | ||
var ieEvt = document.createEventObject(); | ||
return element.fireEvent("on" + event, ieEvt); | ||
} | ||
@@ -78,21 +84,19 @@ } | ||
applyPreview() { | ||
const markdownOptions = this.options['markdownOptions'] | ||
const plugins = this.options['plugins'] | ||
const markdownOptions = this.options["markdownOptions"]; | ||
const plugins = this.options["plugins"]; | ||
if (this.previews) { | ||
this.previews.forEach((preview) => { | ||
let md = new MarkdownIt(markdownOptions); | ||
plugins.forEach((plugin) => md.use(plugin)) | ||
preview.innerHTML = md.render(this.textarea.value); | ||
}) | ||
plugins.forEach((plugin) => md.use(plugin)); | ||
preview.innerHTML = md.render(this.textarea.value); | ||
}); | ||
} | ||
this.options['afterPreview']() | ||
this.options["afterPreview"](); | ||
} | ||
uploadToOriginal(file) { | ||
uploadToOriginal() {} | ||
} | ||
uploadAll(files) { | ||
Array.from(files, f => this.upload(f)); | ||
Array.from(files, (f) => this.upload(f)); | ||
} | ||
@@ -103,44 +107,64 @@ | ||
reader.readAsArrayBuffer(file); | ||
reader.onload = (event) => { | ||
reader.onload = () => { | ||
const bytes = new Uint8Array(reader.result); | ||
const fileType = filetypeextension(bytes)[0]; | ||
const fileSize = filesize(file.size, {base: 10, standard: "jedec"}); | ||
const text = '![' + this.options['placeholder'].replace(/\%filename/, file.name) + ']()'; | ||
const fileSize = filesize(file.size, { base: 10, standard: "jedec" }); | ||
const text = | ||
"![" + | ||
this.options["placeholder"].replace(/\%filename/, file.name) + | ||
"]()"; | ||
const beforeRange = this.textarea.selectionStart; | ||
const afterRange = text.length; | ||
// const afterRange = text.length; | ||
const beforeText = this.textarea.value.substring(0, beforeRange); | ||
const afterText = this.textarea.value.substring(beforeRange, this.textarea.value.length); | ||
const afterText = this.textarea.value.substring( | ||
beforeRange, | ||
this.textarea.value.length | ||
); | ||
this.textarea.value = `${beforeText}\n${text}\n${afterText}`; | ||
let params = new FormData(); | ||
params.append(this.options['paramName'], file); | ||
params.append(this.options["paramName"], file); | ||
let headers = { 'X-Requested-With': 'XMLHttpRequest' }; | ||
if (this.options['csrfToken']) { | ||
headers['X-CSRF-Token'] = this.options['csrfToken']; | ||
let headers = { "X-Requested-With": "XMLHttpRequest" }; | ||
if (this.options["csrfToken"]) { | ||
headers["X-CSRF-Token"] = this.options["csrfToken"]; | ||
} | ||
fetch(this.options['endPoint'], { | ||
method: 'POST', | ||
fetch(this.options["endPoint"], { | ||
method: "POST", | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: params | ||
}).then((response) => { | ||
return response.json(); | ||
}).then((json) => { | ||
const responseKey = this.options['responseKey']; | ||
const url = json[responseKey]; | ||
if(this.options['imageableExtensions'].includes(fileType)) { | ||
this.textarea.value = this.textarea.value.replace(text, `![${file.name}](${url})\n`); | ||
} else { | ||
this.textarea.value = this.textarea.value.replace(text, `[${file.name} (${fileSize})](${url})\n`); | ||
} | ||
this.applyPreview(); | ||
}).catch((error) => { | ||
this.textarea.value = this.textarea.value.replace(text, ''); | ||
console.warn('parsing failed', error) | ||
credentials: "same-origin", | ||
body: params, | ||
}) | ||
.then((response) => { | ||
return response.json(); | ||
}) | ||
.then((json) => { | ||
const responseKey = this.options["responseKey"]; | ||
const url = json[responseKey]; | ||
if (this.options["imageableExtensions"].includes(fileType)) { | ||
this.textarea.value = this.textarea.value.replace( | ||
text, | ||
`![${file.name}](${url})\n` | ||
); | ||
} else if (this.options["videoExtensions"].includes(fileType)) { | ||
this.textarea.value = this.textarea.value.replace( | ||
text, | ||
`<video controls src="${url}"></video>\n` | ||
); | ||
} else { | ||
this.textarea.value = this.textarea.value.replace( | ||
text, | ||
`[${file.name} (${fileSize})](${url})\n` | ||
); | ||
} | ||
this.applyPreview(); | ||
}) | ||
.catch((error) => { | ||
this.textarea.value = this.textarea.value.replace(text, ""); | ||
console.warn("parsing failed", error); | ||
}); | ||
}; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18539
11
456
10