svelte-multistyle-ui 0.0.9-alpha → 0.0.13-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cli/registry.js +2 -0
- package/dist/components/CodeEditor.svelte +130 -0
- package/dist/components/CodeEditor.svelte.d.ts +25 -0
- package/dist/components/TextEditor.svelte +187 -0
- package/dist/components/TextEditor.svelte.d.ts +27 -0
- package/dist/components/charts/chart-config.d.ts +2 -2
- package/dist/components/codeeditor-styles.css +292 -0
- package/dist/components/text-editor-core/dom-utils.d.ts +17 -0
- package/dist/components/text-editor-core/dom-utils.js +151 -0
- package/dist/components/text-editor-core/editor-core.d.ts +113 -0
- package/dist/components/text-editor-core/editor-core.js +808 -0
- package/dist/components/text-editor-core/history.d.ts +14 -0
- package/dist/components/text-editor-core/history.js +78 -0
- package/dist/components/text-editor-core/html-formatter.d.ts +4 -0
- package/dist/components/text-editor-core/html-formatter.js +80 -0
- package/dist/components/text-editor-core/index.d.ts +7 -0
- package/dist/components/text-editor-core/index.js +22 -0
- package/dist/components/text-editor-core/sanitizer.d.ts +3 -0
- package/dist/components/text-editor-core/sanitizer.js +161 -0
- package/dist/components/text-editor-core/selection-engine.d.ts +11 -0
- package/dist/components/text-editor-core/selection-engine.js +137 -0
- package/dist/components/text-editor-core/toolbar-config.d.ts +91 -0
- package/dist/components/text-editor-core/toolbar-config.js +138 -0
- package/dist/components/texteditor-styles.css +493 -0
- package/dist/config.svelte.d.ts +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/themes/presets.d.ts +21 -21
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo/redo history stack for the custom editor.
|
|
3
|
+
*/
|
|
4
|
+
export function createHistory(limit?: number): {
|
|
5
|
+
record: (html: any) => void;
|
|
6
|
+
undo: (currentHtml: any) => any;
|
|
7
|
+
redo: () => any;
|
|
8
|
+
canUndo: () => boolean;
|
|
9
|
+
canRedo: () => boolean;
|
|
10
|
+
startComposition: () => void;
|
|
11
|
+
endComposition: (html: any) => void;
|
|
12
|
+
clear: () => void;
|
|
13
|
+
readonly length: number;
|
|
14
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo/redo history stack for the custom editor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function createHistory(limit = 100) {
|
|
6
|
+
const stack = [];
|
|
7
|
+
let index = -1;
|
|
8
|
+
let composing = false;
|
|
9
|
+
|
|
10
|
+
function record(html) {
|
|
11
|
+
if (composing) return;
|
|
12
|
+
// Avoid duplicate consecutive records.
|
|
13
|
+
if (stack[index]?.html === html) return;
|
|
14
|
+
|
|
15
|
+
// Truncate redo entries when a new action occurs after undo.
|
|
16
|
+
if (index < stack.length - 1) {
|
|
17
|
+
stack.splice(index + 1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
stack.push({ html, time: Date.now() });
|
|
21
|
+
if (stack.length > limit) {
|
|
22
|
+
stack.shift();
|
|
23
|
+
} else {
|
|
24
|
+
index++;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function undo(currentHtml) {
|
|
29
|
+
if (index <= 0) return null;
|
|
30
|
+
if (stack[index].html !== currentHtml) {
|
|
31
|
+
record(currentHtml);
|
|
32
|
+
}
|
|
33
|
+
index--;
|
|
34
|
+
return stack[index]?.html ?? null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function redo() {
|
|
38
|
+
if (index >= stack.length - 1) return null;
|
|
39
|
+
index++;
|
|
40
|
+
return stack[index]?.html ?? null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function canUndo() {
|
|
44
|
+
return index > 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function canRedo() {
|
|
48
|
+
return index < stack.length - 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function startComposition() {
|
|
52
|
+
composing = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function endComposition(html) {
|
|
56
|
+
composing = false;
|
|
57
|
+
record(html);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function clear() {
|
|
61
|
+
stack.length = 0;
|
|
62
|
+
index = -1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
record,
|
|
67
|
+
undo,
|
|
68
|
+
redo,
|
|
69
|
+
canUndo,
|
|
70
|
+
canRedo,
|
|
71
|
+
startComposition,
|
|
72
|
+
endComposition,
|
|
73
|
+
clear,
|
|
74
|
+
get length() {
|
|
75
|
+
return stack.length;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight HTML formatter used by the code-view textarea.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function formatHTML(html) {
|
|
6
|
+
if (!html) return "";
|
|
7
|
+
const parser = new DOMParser();
|
|
8
|
+
const doc = parser.parseFromString(`<div>${html}</div>`, "text/html");
|
|
9
|
+
const root = doc.body.firstChild;
|
|
10
|
+
if (!root) return html;
|
|
11
|
+
|
|
12
|
+
const children = Array.from(root.childNodes);
|
|
13
|
+
if (!children.length) return html;
|
|
14
|
+
|
|
15
|
+
let result = "";
|
|
16
|
+
for (const child of children) {
|
|
17
|
+
result += formatNode(child, 0);
|
|
18
|
+
}
|
|
19
|
+
return result.trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function formatNode(node, depth) {
|
|
23
|
+
const indent = " ".repeat(depth);
|
|
24
|
+
|
|
25
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
26
|
+
const text = node.textContent || "";
|
|
27
|
+
return text.trim() ? `\n${indent}${text.trim()}` : "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (node.nodeType !== Node.ELEMENT_NODE) return "";
|
|
31
|
+
|
|
32
|
+
const tag = node.tagName.toLowerCase();
|
|
33
|
+
const isVoid = ["br", "hr", "img", "input"].includes(tag);
|
|
34
|
+
const children = Array.from(node.childNodes);
|
|
35
|
+
|
|
36
|
+
const attrs = formatAttrs(node);
|
|
37
|
+
const open = `<${tag}${attrs}${isVoid ? " /" : ""}>`;
|
|
38
|
+
if (isVoid) return `\n${indent}${open}`;
|
|
39
|
+
|
|
40
|
+
const hasBlockChildren = children.some((c) => c.nodeType === Node.ELEMENT_NODE && isBlock(c.tagName));
|
|
41
|
+
let inner = "";
|
|
42
|
+
|
|
43
|
+
if (hasBlockChildren) {
|
|
44
|
+
for (const child of children) {
|
|
45
|
+
inner += formatNode(child, depth + 1);
|
|
46
|
+
}
|
|
47
|
+
inner += `\n${indent}`;
|
|
48
|
+
} else {
|
|
49
|
+
const textContent = node.innerHTML;
|
|
50
|
+
if (textContent.trim()) {
|
|
51
|
+
inner = textContent;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const close = `</${tag}>`;
|
|
56
|
+
return `\n${indent}${open}${inner}${close}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function formatAttrs(el) {
|
|
60
|
+
const attrs = Array.from(el.attributes);
|
|
61
|
+
if (!attrs.length) return "";
|
|
62
|
+
return attrs
|
|
63
|
+
.map((a) => {
|
|
64
|
+
const name = a.name;
|
|
65
|
+
const value = a.value
|
|
66
|
+
.replace(/&/g, "&")
|
|
67
|
+
.replace(/"/g, """);
|
|
68
|
+
return value === "" ? ` ${name}` : ` ${name}="${value}"`;
|
|
69
|
+
})
|
|
70
|
+
.join("");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isBlock(tagName) {
|
|
74
|
+
const blocks = [
|
|
75
|
+
"P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6",
|
|
76
|
+
"BLOCKQUOTE", "PRE", "UL", "OL", "LI", "TABLE",
|
|
77
|
+
"THEAD", "TBODY", "TR", "TD", "TH", "FIGURE", "FIGCAPTION",
|
|
78
|
+
];
|
|
79
|
+
return blocks.includes(tagName);
|
|
80
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { TextEditorCore } from "./editor-core.js";
|
|
2
|
+
export { getSelectionState } from "./selection-engine.js";
|
|
3
|
+
export { createHistory } from "./history.js";
|
|
4
|
+
export { formatHTML } from "./html-formatter.js";
|
|
5
|
+
export { sanitizeHTML, plainTextFromHTML, sanitizeURL } from "./sanitizer.js";
|
|
6
|
+
export { createElement, closest, getWordCount, getTextLength, htmlToText } from "./dom-utils.js";
|
|
7
|
+
export { DEFAULT_TOOLBAR, FLOATING_TOOLBAR, FONT_FAMILIES, FONT_SIZES, HEADING_OPTIONS, LINE_HEIGHTS, LIST_TYPES } from "./toolbar-config.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { TextEditorCore } from "./editor-core.js";
|
|
2
|
+
export { getSelectionState } from "./selection-engine.js";
|
|
3
|
+
export { sanitizeHTML, plainTextFromHTML } from "./sanitizer.js";
|
|
4
|
+
export { createHistory } from "./history.js";
|
|
5
|
+
export {
|
|
6
|
+
createElement,
|
|
7
|
+
closest,
|
|
8
|
+
getWordCount,
|
|
9
|
+
getTextLength,
|
|
10
|
+
htmlToText,
|
|
11
|
+
} from "./dom-utils.js";
|
|
12
|
+
export { sanitizeURL } from "./sanitizer.js";
|
|
13
|
+
export { formatHTML } from "./html-formatter.js";
|
|
14
|
+
export {
|
|
15
|
+
DEFAULT_TOOLBAR,
|
|
16
|
+
FLOATING_TOOLBAR,
|
|
17
|
+
FONT_FAMILIES,
|
|
18
|
+
FONT_SIZES,
|
|
19
|
+
HEADING_OPTIONS,
|
|
20
|
+
LINE_HEIGHTS,
|
|
21
|
+
LIST_TYPES,
|
|
22
|
+
} from "./toolbar-config.js";
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML sanitizer for the custom editor.
|
|
3
|
+
* Parses HTML, keeps allowed tags/attributes, removes forbidden tags entirely.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const ALLOWED_TAGS = new Set([
|
|
7
|
+
"p", "div", "span", "br", "strong", "em", "u", "s", "sup", "sub",
|
|
8
|
+
"ul", "ol", "li", "blockquote", "pre", "code",
|
|
9
|
+
"table", "thead", "tbody", "tr", "td", "th", "caption",
|
|
10
|
+
"img", "a", "iframe", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
const FORBIDDEN_TAGS = new Set([
|
|
14
|
+
"script", "style", "object", "embed", "form", "input", "button", "textarea",
|
|
15
|
+
"select", "option", "canvas", "svg", "math",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
const ALLOWED_ATTRS = new Set([
|
|
19
|
+
"href", "title", "target", "rel",
|
|
20
|
+
"src", "alt", "width", "height",
|
|
21
|
+
"align", "class", "style",
|
|
22
|
+
"colspan", "rowspan",
|
|
23
|
+
"frameborder", "allowfullscreen",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
const ALLOWED_CSS_PROPS = new Set([
|
|
27
|
+
"color", "background-color", "background", "font-family", "font-size",
|
|
28
|
+
"text-align", "vertical-align", "line-height", "text-decoration",
|
|
29
|
+
"font-weight", "font-style",
|
|
30
|
+
"width", "height", "max-width", "max-height",
|
|
31
|
+
"float", "margin", "margin-left", "margin-right", "padding",
|
|
32
|
+
"border", "border-collapse", "border-spacing",
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const FORMAT_NORMALIZE_MAP = {
|
|
36
|
+
strong: "b",
|
|
37
|
+
em: "i",
|
|
38
|
+
strike: "s",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export function sanitizeHTML(html, options = {}) {
|
|
42
|
+
if (!html) return "";
|
|
43
|
+
const parser = new DOMParser();
|
|
44
|
+
const doc = parser.parseFromString(html, "text/html");
|
|
45
|
+
cleanNode(doc.body);
|
|
46
|
+
if (options.normalizeFormats !== false) {
|
|
47
|
+
normalizeInlineFormats(doc.body);
|
|
48
|
+
}
|
|
49
|
+
return doc.body.innerHTML;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function normalizeInlineFormats(root) {
|
|
53
|
+
// Convert <strong> → <b>, <em> → <i>, <strike> → <s>
|
|
54
|
+
for (const [from, to] of Object.entries(FORMAT_NORMALIZE_MAP)) {
|
|
55
|
+
const nodes = Array.from(root.querySelectorAll(from));
|
|
56
|
+
for (const node of nodes) {
|
|
57
|
+
const replacement = document.createElement(to);
|
|
58
|
+
while (node.firstChild) {
|
|
59
|
+
replacement.appendChild(node.firstChild);
|
|
60
|
+
}
|
|
61
|
+
node.parentNode.replaceChild(replacement, node);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Convert inline style-based formatting to semantic tags when possible.
|
|
66
|
+
const spans = Array.from(root.querySelectorAll("span[style]"));
|
|
67
|
+
for (const span of spans) {
|
|
68
|
+
const style = span.style;
|
|
69
|
+
let replacementTag = null;
|
|
70
|
+
if (style.fontWeight === "bold" || Number(style.fontWeight) >= 700) {
|
|
71
|
+
replacementTag = "b";
|
|
72
|
+
} else if (style.fontStyle === "italic") {
|
|
73
|
+
replacementTag = "i";
|
|
74
|
+
} else if (style.textDecorationLine?.includes("line-through")) {
|
|
75
|
+
replacementTag = "s";
|
|
76
|
+
} else if (style.verticalAlign === "super") {
|
|
77
|
+
replacementTag = "sup";
|
|
78
|
+
} else if (style.verticalAlign === "sub") {
|
|
79
|
+
replacementTag = "sub";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (replacementTag) {
|
|
83
|
+
const replacement = document.createElement(replacementTag);
|
|
84
|
+
while (span.firstChild) {
|
|
85
|
+
replacement.appendChild(span.firstChild);
|
|
86
|
+
}
|
|
87
|
+
span.parentNode.replaceChild(replacement, span);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function cleanNode(node) {
|
|
93
|
+
const children = Array.from(node.childNodes);
|
|
94
|
+
for (const child of children) {
|
|
95
|
+
if (child.nodeType === Node.TEXT_NODE) continue;
|
|
96
|
+
|
|
97
|
+
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
98
|
+
const tag = child.tagName.toLowerCase();
|
|
99
|
+
|
|
100
|
+
if (FORBIDDEN_TAGS.has(tag)) {
|
|
101
|
+
node.removeChild(child);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!ALLOWED_TAGS.has(tag)) {
|
|
106
|
+
// Unwrap unknown tags (keep children).
|
|
107
|
+
const parent = child.parentNode;
|
|
108
|
+
while (child.firstChild) {
|
|
109
|
+
parent.insertBefore(child.firstChild, child);
|
|
110
|
+
}
|
|
111
|
+
parent.removeChild(child);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Clean attributes.
|
|
116
|
+
const attrs = Array.from(child.attributes);
|
|
117
|
+
for (const attr of attrs) {
|
|
118
|
+
if (!ALLOWED_ATTRS.has(attr.name.toLowerCase())) {
|
|
119
|
+
child.removeAttribute(attr.name);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (attr.name.toLowerCase() === "style") {
|
|
123
|
+
child.setAttribute("style", sanitizeInlineStyles(attr.value));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
cleanNode(child);
|
|
128
|
+
} else {
|
|
129
|
+
// Remove comments and other non-element/text nodes.
|
|
130
|
+
node.removeChild(child);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function sanitizeInlineStyles(styleValue) {
|
|
136
|
+
if (!styleValue) return "";
|
|
137
|
+
const declarations = styleValue.split(";");
|
|
138
|
+
const cleaned = [];
|
|
139
|
+
for (const decl of declarations) {
|
|
140
|
+
const colonIdx = decl.indexOf(":");
|
|
141
|
+
if (colonIdx === -1) continue;
|
|
142
|
+
const prop = decl.slice(0, colonIdx).trim().toLowerCase();
|
|
143
|
+
const value = decl.slice(colonIdx + 1).trim();
|
|
144
|
+
if (ALLOWED_CSS_PROPS.has(prop) && value && !/javascript:/i.test(value)) {
|
|
145
|
+
cleaned.push(`${prop}: ${value}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return cleaned.join("; ");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function sanitizeURL(url) {
|
|
152
|
+
if (!url) return "";
|
|
153
|
+
if (/^javascript:/i.test(url)) return "";
|
|
154
|
+
return url;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function plainTextFromHTML(html) {
|
|
158
|
+
const div = document.createElement("div");
|
|
159
|
+
div.innerHTML = html;
|
|
160
|
+
return div.textContent || "";
|
|
161
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function getSelectionState(editorEl: any): {
|
|
2
|
+
collapsed: boolean;
|
|
3
|
+
startNode: null;
|
|
4
|
+
endNode: null;
|
|
5
|
+
selectedText: string;
|
|
6
|
+
activeFormats: Set<any>;
|
|
7
|
+
currentTag: string;
|
|
8
|
+
range: null;
|
|
9
|
+
};
|
|
10
|
+
export function restoreRange(range: any): void;
|
|
11
|
+
export function withSavedSelection(fn: any): any;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selection state tracking for the custom editor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { getParentBlock, isInsideEditor } from "./dom-utils.js";
|
|
6
|
+
|
|
7
|
+
export function getSelectionState(editorEl) {
|
|
8
|
+
const sel = window.getSelection();
|
|
9
|
+
const state = {
|
|
10
|
+
collapsed: true,
|
|
11
|
+
startNode: null,
|
|
12
|
+
endNode: null,
|
|
13
|
+
selectedText: "",
|
|
14
|
+
activeFormats: new Set(),
|
|
15
|
+
currentTag: "",
|
|
16
|
+
range: null,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
if (!sel || sel.rangeCount === 0) return state;
|
|
20
|
+
|
|
21
|
+
const range = sel.getRangeAt(0);
|
|
22
|
+
const anchorIn = isInsideEditor(sel.anchorNode, editorEl);
|
|
23
|
+
const focusIn = isInsideEditor(sel.focusNode, editorEl);
|
|
24
|
+
|
|
25
|
+
if (!anchorIn || !focusIn) return state;
|
|
26
|
+
|
|
27
|
+
state.collapsed = range.collapsed;
|
|
28
|
+
state.startNode = sel.anchorNode;
|
|
29
|
+
state.endNode = sel.focusNode;
|
|
30
|
+
state.selectedText = sel.toString();
|
|
31
|
+
state.range = range;
|
|
32
|
+
state.currentTag = getCurrentBlockTag(sel.anchorNode, editorEl);
|
|
33
|
+
|
|
34
|
+
state.activeFormats = detectActiveFormats(range, editorEl);
|
|
35
|
+
|
|
36
|
+
return state;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getCurrentBlockTag(node, editorEl) {
|
|
40
|
+
const block = getParentBlock(node, editorEl);
|
|
41
|
+
return block ? block.tagName.toLowerCase() : "";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function detectActiveFormats(range, editorEl) {
|
|
45
|
+
const formats = new Set();
|
|
46
|
+
if (range.collapsed) {
|
|
47
|
+
// For collapsed selection, check parent chain.
|
|
48
|
+
let node = range.startContainer;
|
|
49
|
+
while (node && node !== editorEl) {
|
|
50
|
+
addFormatFromNode(formats, node);
|
|
51
|
+
node = node.parentNode;
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
// For a range, scan all text nodes inside the range.
|
|
55
|
+
const walker = document.createTreeWalker(
|
|
56
|
+
editorEl,
|
|
57
|
+
NodeFilter.SHOW_TEXT,
|
|
58
|
+
null,
|
|
59
|
+
false,
|
|
60
|
+
);
|
|
61
|
+
let node;
|
|
62
|
+
while ((node = walker.nextNode())) {
|
|
63
|
+
if (range.intersectsNode(node)) {
|
|
64
|
+
let parent = node.parentNode;
|
|
65
|
+
while (parent && parent !== editorEl) {
|
|
66
|
+
addFormatFromNode(formats, parent);
|
|
67
|
+
parent = parent.parentNode;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return formats;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function addFormatFromNode(formats, node) {
|
|
76
|
+
if (!node || node.nodeType !== Node.ELEMENT_NODE) return;
|
|
77
|
+
const tag = node.tagName.toLowerCase();
|
|
78
|
+
|
|
79
|
+
const tagMap = {
|
|
80
|
+
strong: "bold",
|
|
81
|
+
b: "bold",
|
|
82
|
+
em: "italic",
|
|
83
|
+
i: "italic",
|
|
84
|
+
u: "underline",
|
|
85
|
+
s: "strikeThrough",
|
|
86
|
+
strike: "strikeThrough",
|
|
87
|
+
sup: "superscript",
|
|
88
|
+
sub: "subscript",
|
|
89
|
+
a: "link",
|
|
90
|
+
h1: "formatH1",
|
|
91
|
+
h2: "formatH2",
|
|
92
|
+
h3: "formatH3",
|
|
93
|
+
h4: "formatH4",
|
|
94
|
+
h5: "formatH5",
|
|
95
|
+
h6: "formatH6",
|
|
96
|
+
p: "formatP",
|
|
97
|
+
blockquote: "formatBlockquote",
|
|
98
|
+
pre: "formatPre",
|
|
99
|
+
ul: "insertUnorderedList",
|
|
100
|
+
ol: "insertOrderedList",
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
if (tagMap[tag]) formats.add(tagMap[tag]);
|
|
104
|
+
|
|
105
|
+
const style = node.style || {};
|
|
106
|
+
if (style.fontWeight === "bold" || Number(style.fontWeight) >= 700) {
|
|
107
|
+
formats.add("bold");
|
|
108
|
+
}
|
|
109
|
+
if (style.fontStyle === "italic") formats.add("italic");
|
|
110
|
+
if (style.textDecorationLine?.includes("underline")) formats.add("underline");
|
|
111
|
+
if (style.textDecorationLine?.includes("line-through")) formats.add("strikeThrough");
|
|
112
|
+
if (style.verticalAlign === "super") formats.add("superscript");
|
|
113
|
+
if (style.verticalAlign === "sub") formats.add("subscript");
|
|
114
|
+
|
|
115
|
+
const align = style.textAlign || node.getAttribute("align");
|
|
116
|
+
if (align === "left") formats.add("justifyLeft");
|
|
117
|
+
if (align === "center") formats.add("justifyCenter");
|
|
118
|
+
if (align === "right") formats.add("justifyRight");
|
|
119
|
+
if (align === "justify") formats.add("justifyFull");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function restoreRange(range) {
|
|
123
|
+
if (!range) return;
|
|
124
|
+
const sel = window.getSelection();
|
|
125
|
+
sel.removeAllRanges();
|
|
126
|
+
sel.addRange(range);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function withSavedSelection(fn) {
|
|
130
|
+
const sel = window.getSelection();
|
|
131
|
+
const range = sel.rangeCount > 0 ? sel.getRangeAt(0).cloneRange() : null;
|
|
132
|
+
try {
|
|
133
|
+
return fn();
|
|
134
|
+
} finally {
|
|
135
|
+
if (range) restoreRange(range);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default toolbar configuration for the custom editor.
|
|
3
|
+
* Each group is an array of action items rendered in the toolbar.
|
|
4
|
+
*/
|
|
5
|
+
export const FONT_FAMILIES: {
|
|
6
|
+
label: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}[];
|
|
9
|
+
export const FONT_SIZES: {
|
|
10
|
+
label: string;
|
|
11
|
+
value: string;
|
|
12
|
+
}[];
|
|
13
|
+
export const HEADING_OPTIONS: {
|
|
14
|
+
label: string;
|
|
15
|
+
value: string;
|
|
16
|
+
}[];
|
|
17
|
+
export const LINE_HEIGHTS: {
|
|
18
|
+
label: string;
|
|
19
|
+
value: string;
|
|
20
|
+
}[];
|
|
21
|
+
export namespace LIST_TYPES {
|
|
22
|
+
let unordered: {
|
|
23
|
+
label: string;
|
|
24
|
+
value: string;
|
|
25
|
+
}[];
|
|
26
|
+
let ordered: {
|
|
27
|
+
label: string;
|
|
28
|
+
value: string;
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Toolbar action definitions. Each action has:
|
|
33
|
+
* - id: unique identifier
|
|
34
|
+
* - type: 'button' | 'select' | 'color' | 'separator'
|
|
35
|
+
* - icon / label: UI representation
|
|
36
|
+
* - command: execCommand name or custom handler key
|
|
37
|
+
* - active: format key for selection engine
|
|
38
|
+
*/
|
|
39
|
+
export const DEFAULT_TOOLBAR: ({
|
|
40
|
+
id: string;
|
|
41
|
+
type: string;
|
|
42
|
+
label: string;
|
|
43
|
+
title: string;
|
|
44
|
+
command: string;
|
|
45
|
+
icon: string;
|
|
46
|
+
}[] | ({
|
|
47
|
+
id: string;
|
|
48
|
+
type: string;
|
|
49
|
+
title: string;
|
|
50
|
+
command: string;
|
|
51
|
+
options: {
|
|
52
|
+
label: string;
|
|
53
|
+
value: string;
|
|
54
|
+
}[];
|
|
55
|
+
label?: undefined;
|
|
56
|
+
active?: undefined;
|
|
57
|
+
} | {
|
|
58
|
+
id: string;
|
|
59
|
+
type: string;
|
|
60
|
+
label: string;
|
|
61
|
+
title: string;
|
|
62
|
+
command: string;
|
|
63
|
+
active: string;
|
|
64
|
+
options?: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
id: string;
|
|
67
|
+
type: string;
|
|
68
|
+
label: string;
|
|
69
|
+
title: string;
|
|
70
|
+
command: string;
|
|
71
|
+
options?: undefined;
|
|
72
|
+
active?: undefined;
|
|
73
|
+
})[])[];
|
|
74
|
+
/**
|
|
75
|
+
* Floating (air-mode) toolbar actions shown on text selection.
|
|
76
|
+
*/
|
|
77
|
+
export const FLOATING_TOOLBAR: ({
|
|
78
|
+
id: string;
|
|
79
|
+
type: string;
|
|
80
|
+
label: string;
|
|
81
|
+
title: string;
|
|
82
|
+
command: string;
|
|
83
|
+
active: string;
|
|
84
|
+
} | {
|
|
85
|
+
id: string;
|
|
86
|
+
type: string;
|
|
87
|
+
label: string;
|
|
88
|
+
title: string;
|
|
89
|
+
command: string;
|
|
90
|
+
active?: undefined;
|
|
91
|
+
})[];
|