diff --git a/ts/editor/filterHtml.ts b/ts/editor/htmlFilter.ts similarity index 99% rename from ts/editor/filterHtml.ts rename to ts/editor/htmlFilter.ts index 0b142932d..bb995cbd9 100644 --- a/ts/editor/filterHtml.ts +++ b/ts/editor/htmlFilter.ts @@ -1,6 +1,6 @@ import { nodeIsElement } from "./helpers"; -export let filterHTML = function ( +export function filterHTML( html: string, internal: boolean, extendedMode: boolean @@ -21,7 +21,7 @@ export let filterHTML = function ( } outHtml = outHtml.trim(); return outHtml; -}; +} let allowedTagsBasic = {}; let allowedTagsExtended = {}; diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 81f42b5e6..1483fbb48 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -1,10 +1,10 @@ /* Copyright: Ankitects Pty Ltd and contributors * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ -import { filterHTML } from "./filterHtml"; import { nodeIsInline } from "./helpers"; import { bridgeCommand } from "./lib"; import { saveField } from "./changeTimer"; +import { filterHTML } from "./htmlFilter"; import { updateButtonState, maybeDisableButtons } from "./toolbar"; import { onInput, onKey, onKeyUp } from "./inputHandlers"; import { onFocus, onBlur } from "./focusHandlers"; @@ -12,6 +12,7 @@ import { onFocus, onBlur } from "./focusHandlers"; export { setNoteId, getNoteId } from "./noteId"; export { preventButtonFocus, toggleEditorButton, setFGButton } from "./toolbar"; export { saveNow } from "./changeTimer"; +export { wrap, wrapIntoText } from "./wrap"; declare global { interface Selection { @@ -48,6 +49,18 @@ export function focusIfField(x: number, y: number): boolean { return false; } +export function pasteHTML( + html: string, + internal: boolean, + extendedMode: boolean +): void { + html = filterHTML(html, internal, extendedMode); + + if (html !== "") { + setFormat("inserthtml", html); + } +} + function onPaste(evt: ClipboardEvent): void { bridgeCommand("paste"); evt.preventDefault(); @@ -304,20 +317,6 @@ export function setFonts(fonts: [string, number, boolean][]): void { }); } -function wrappedExceptForWhitespace(text: string, front: string, back: string): string { - const match = text.match(/^(\s*)([^]*?)(\s*)$/)!; - return match[1] + front + match[2] + back + match[3]; -} - -export function wrap(front: string, back: string): void { - wrapInternal(front, back, false); -} - -/* currently unused */ -export function wrapIntoText(front: string, back: string): void { - wrapInternal(front, back, true); -} - export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void { document.execCommand(cmd, false, arg); if (!nosave) { @@ -325,41 +324,3 @@ export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void updateButtonState(); } } - -function wrapInternal(front: string, back: string, plainText: boolean): void { - const currentField = getCurrentField()!; - const s = currentField.getSelection(); - let r = s.getRangeAt(0); - const content = r.cloneContents(); - const span = document.createElement("span"); - span.appendChild(content); - - if (plainText) { - const new_ = wrappedExceptForWhitespace(span.innerText, front, back); - setFormat("inserttext", new_); - } else { - const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); - setFormat("inserthtml", new_); - } - - if (!span.innerHTML) { - // run with an empty selection; move cursor back past postfix - r = s.getRangeAt(0); - r.setStart(r.startContainer, r.startOffset - back.length); - r.collapse(true); - s.removeAllRanges(); - s.addRange(r); - } -} - -export let pasteHTML = function ( - html: string, - internal: boolean, - extendedMode: boolean -): void { - html = filterHTML(html, internal, extendedMode); - - if (html !== "") { - setFormat("inserthtml", html); - } -}; diff --git a/ts/editor/wrap.ts b/ts/editor/wrap.ts new file mode 100644 index 000000000..33fcdf742 --- /dev/null +++ b/ts/editor/wrap.ts @@ -0,0 +1,41 @@ +import { getCurrentField, setFormat } from "."; + +function wrappedExceptForWhitespace(text: string, front: string, back: string): string { + const match = text.match(/^(\s*)([^]*?)(\s*)$/)!; + return match[1] + front + match[2] + back + match[3]; +} + +function wrapInternal(front: string, back: string, plainText: boolean): void { + const currentField = getCurrentField()!; + const s = currentField.getSelection(); + let r = s.getRangeAt(0); + const content = r.cloneContents(); + const span = document.createElement("span"); + span.appendChild(content); + + if (plainText) { + const new_ = wrappedExceptForWhitespace(span.innerText, front, back); + setFormat("inserttext", new_); + } else { + const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); + setFormat("inserthtml", new_); + } + + if (!span.innerHTML) { + // run with an empty selection; move cursor back past postfix + r = s.getRangeAt(0); + r.setStart(r.startContainer, r.startOffset - back.length); + r.collapse(true); + s.removeAllRanges(); + s.addRange(r); + } +} + +export function wrap(front: string, back: string): void { + wrapInternal(front, back, false); +} + +/* currently unused */ +export function wrapIntoText(front: string, back: string): void { + wrapInternal(front, back, true); +}