mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Put wrapping code into its own file
This commit is contained in:
parent
a19bc2d012
commit
61c4ef40de
3 changed files with 57 additions and 55 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { nodeIsElement } from "./helpers";
|
import { nodeIsElement } from "./helpers";
|
||||||
|
|
||||||
export let filterHTML = function (
|
export function filterHTML(
|
||||||
html: string,
|
html: string,
|
||||||
internal: boolean,
|
internal: boolean,
|
||||||
extendedMode: boolean
|
extendedMode: boolean
|
||||||
|
@ -21,7 +21,7 @@ export let filterHTML = function (
|
||||||
}
|
}
|
||||||
outHtml = outHtml.trim();
|
outHtml = outHtml.trim();
|
||||||
return outHtml;
|
return outHtml;
|
||||||
};
|
}
|
||||||
|
|
||||||
let allowedTagsBasic = {};
|
let allowedTagsBasic = {};
|
||||||
let allowedTagsExtended = {};
|
let allowedTagsExtended = {};
|
|
@ -1,10 +1,10 @@
|
||||||
/* Copyright: Ankitects Pty Ltd and contributors
|
/* Copyright: Ankitects Pty Ltd and contributors
|
||||||
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
||||||
|
|
||||||
import { filterHTML } from "./filterHtml";
|
|
||||||
import { nodeIsInline } from "./helpers";
|
import { nodeIsInline } from "./helpers";
|
||||||
import { bridgeCommand } from "./lib";
|
import { bridgeCommand } from "./lib";
|
||||||
import { saveField } from "./changeTimer";
|
import { saveField } from "./changeTimer";
|
||||||
|
import { filterHTML } from "./htmlFilter";
|
||||||
import { updateButtonState, maybeDisableButtons } from "./toolbar";
|
import { updateButtonState, maybeDisableButtons } from "./toolbar";
|
||||||
import { onInput, onKey, onKeyUp } from "./inputHandlers";
|
import { onInput, onKey, onKeyUp } from "./inputHandlers";
|
||||||
import { onFocus, onBlur } from "./focusHandlers";
|
import { onFocus, onBlur } from "./focusHandlers";
|
||||||
|
@ -12,6 +12,7 @@ import { onFocus, onBlur } from "./focusHandlers";
|
||||||
export { setNoteId, getNoteId } from "./noteId";
|
export { setNoteId, getNoteId } from "./noteId";
|
||||||
export { preventButtonFocus, toggleEditorButton, setFGButton } from "./toolbar";
|
export { preventButtonFocus, toggleEditorButton, setFGButton } from "./toolbar";
|
||||||
export { saveNow } from "./changeTimer";
|
export { saveNow } from "./changeTimer";
|
||||||
|
export { wrap, wrapIntoText } from "./wrap";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Selection {
|
interface Selection {
|
||||||
|
@ -48,6 +49,18 @@ export function focusIfField(x: number, y: number): boolean {
|
||||||
return false;
|
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 {
|
function onPaste(evt: ClipboardEvent): void {
|
||||||
bridgeCommand("paste");
|
bridgeCommand("paste");
|
||||||
evt.preventDefault();
|
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 {
|
export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void {
|
||||||
document.execCommand(cmd, false, arg);
|
document.execCommand(cmd, false, arg);
|
||||||
if (!nosave) {
|
if (!nosave) {
|
||||||
|
@ -325,41 +324,3 @@ export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void
|
||||||
updateButtonState();
|
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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
41
ts/editor/wrap.ts
Normal file
41
ts/editor/wrap.ts
Normal file
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in a new issue