kyle-editor 1.0.1 → 1.0.3
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 +180 -22
- package/dist/keditor.cjs +993 -221
- package/dist/keditor.es.js +993 -221
- package/dist/keditor.umd.js +993 -221
- package/package.json +3 -5
- package/types/index.d.ts +182 -5
package/dist/keditor.es.js
CHANGED
|
@@ -10,6 +10,7 @@ const CSS$1 = `
|
|
|
10
10
|
--ke-bg: #f6f7f9;
|
|
11
11
|
--ke-radius: 10px;
|
|
12
12
|
--ke-paper: 677px;
|
|
13
|
+
--ke-paper-gap: 24px;
|
|
13
14
|
--ke-shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
|
|
14
15
|
font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
|
15
16
|
color: var(--ke-text);
|
|
@@ -199,6 +200,43 @@ const CSS$1 = `
|
|
|
199
200
|
.ke-size-cell:hover { border-color: var(--ke-primary); color: var(--ke-primary); background: var(--ke-primary-weak); }
|
|
200
201
|
.ke-size-cell.ke-is-active { border-color: var(--ke-primary); background: var(--ke-primary); color: #fff; }
|
|
201
202
|
|
|
203
|
+
/* -------- 自定义下拉按钮 -------- */
|
|
204
|
+
.ke-btn-custom .ke-btn-text { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
205
|
+
.ke-btn-custom.ke-has-value { color: var(--ke-primary); }
|
|
206
|
+
.ke-panel-icon { margin-left: auto; width: 22px; height: 22px; padding: 0; border: 1px solid var(--ke-line); border-radius: 50%; background: #fff; color: #6b7280; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
|
|
207
|
+
.ke-panel-icon:hover { border-color: var(--ke-primary); color: var(--ke-primary); }
|
|
208
|
+
.ke-panel-icon .ke-ico { width: 13px; height: 13px; }
|
|
209
|
+
.ke-panel-icon + .ke-panel-note { margin-left: 8px; }
|
|
210
|
+
|
|
211
|
+
.ke-panel-select .ke-sel-search { margin-bottom: 8px; }
|
|
212
|
+
.ke-sel-body { max-height: 268px; overflow-y: auto; overscroll-behavior: contain; margin: 0 -4px; padding: 0 4px; }
|
|
213
|
+
.ke-sel-row {
|
|
214
|
+
display: flex; align-items: center; gap: 8px; width: 100%;
|
|
215
|
+
padding: 7px 9px; margin-bottom: 3px; border: 1px solid transparent; border-radius: 8px;
|
|
216
|
+
background: #fff; cursor: pointer; text-align: left; transition: background .14s, border-color .14s;
|
|
217
|
+
}
|
|
218
|
+
.ke-sel-row:last-child { margin-bottom: 0; }
|
|
219
|
+
.ke-sel-row:hover:not(:disabled) { background: #f7f8fa; }
|
|
220
|
+
.ke-sel-row:disabled { cursor: not-allowed; opacity: .45; }
|
|
221
|
+
.ke-sel-row.ke-is-active { border-color: var(--ke-primary); background: var(--ke-primary-weak); }
|
|
222
|
+
.ke-sel-main { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; }
|
|
223
|
+
.ke-sel-label { color: #1f2329; font-size: 13.5px; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
224
|
+
.ke-sel-row.ke-is-active .ke-sel-label { color: var(--ke-primary); font-weight: 600; }
|
|
225
|
+
.ke-sel-desc { color: #a2a8b3; font-size: 11.5px; line-height: 1.45; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
226
|
+
.ke-sel-link { display: inline-flex; align-items: center; gap: 3px; color: #2f6fed; font-size: 11px; }
|
|
227
|
+
.ke-sel-link .ke-ico { width: 11px; height: 11px; flex: none; }
|
|
228
|
+
.ke-sel-link i { font-style: normal; opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; }
|
|
229
|
+
.ke-sel-check { flex: none; width: 16px; height: 16px; color: var(--ke-primary); }
|
|
230
|
+
.ke-sel-check .ke-ico { width: 16px; height: 16px; }
|
|
231
|
+
|
|
232
|
+
.ke-sel-loading { display: flex; align-items: center; gap: 8px; padding: 4px 2px 10px; color: #6b7280; font-size: 12.5px; }
|
|
233
|
+
.ke-sel-loading .ke-spinner { width: 14px; height: 14px; }
|
|
234
|
+
.ke-sel-skel { height: 34px; margin-bottom: 6px; border-radius: 8px; background: linear-gradient(90deg, #f2f3f5 25%, #e9ebef 37%, #f2f3f5 63%); background-size: 400% 100%; animation: ke-skel 1.3s ease infinite; }
|
|
235
|
+
@keyframes ke-skel { from { background-position: 100% 50%; } to { background-position: 0 50%; } }
|
|
236
|
+
.ke-sel-tip { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 22px 12px; text-align: center; }
|
|
237
|
+
.ke-sel-tip-text { color: #a2a8b3; font-size: 12.5px; line-height: 1.6; word-break: break-all; }
|
|
238
|
+
.ke-sel-tip .ke-btn-primary { align-self: center; }
|
|
239
|
+
|
|
202
240
|
.ke-tabs { display: flex; gap: 4px; margin: -12px -14px 12px; padding: 0 14px; background: #f8f9fb; border-bottom: 1px solid var(--ke-line-weak); }
|
|
203
241
|
.ke-tab { height: 34px; padding: 0 4px; margin-right: 10px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: #6b7280; font-size: 13px; cursor: pointer; }
|
|
204
242
|
.ke-tab:hover { color: #1f2329; }
|
|
@@ -245,6 +283,11 @@ const CSS$1 = `
|
|
|
245
283
|
border-radius: 8px; box-shadow: var(--ke-shadow); padding: 32px;
|
|
246
284
|
min-height: var(--ke-min-height, 320px);
|
|
247
285
|
}
|
|
286
|
+
/* 宽屏:版心随容器生长,两侧留白恒定为 --ke-paper-gap,不再随屏宽放大 */
|
|
287
|
+
.ke-paper.ke-is-fluid {
|
|
288
|
+
width: min(100%, max(calc(var(--ke-paper) + 64px), calc(100% - var(--ke-paper-gap) * 2)));
|
|
289
|
+
max-width: var(--ke-paper-max, none);
|
|
290
|
+
}
|
|
248
291
|
.ke-content { outline: none; font-size: 17px; line-height: 1.75; color: var(--ke-text); word-break: break-word; }
|
|
249
292
|
.ke-content:empty::before,
|
|
250
293
|
.ke-content[data-placeholder].ke-is-empty::before {
|
|
@@ -642,8 +685,8 @@ class Popover {
|
|
|
642
685
|
this.el.classList.remove("ke-pop-in");
|
|
643
686
|
void this.el.offsetWidth;
|
|
644
687
|
this.el.classList.add("ke-pop-in");
|
|
645
|
-
const
|
|
646
|
-
(_a =
|
|
688
|
+
const first2 = [...this.el.querySelectorAll("[data-k-autofocus]")].find((node) => node.offsetParent !== null);
|
|
689
|
+
(_a = first2 == null ? void 0 : first2.focus) == null ? void 0 : _a.call(first2);
|
|
647
690
|
}
|
|
648
691
|
close() {
|
|
649
692
|
if (this.el.hidden) return;
|
|
@@ -989,6 +1032,121 @@ function linkPanel({ url = "", text = "", target = false, onSubmit, onCancel, on
|
|
|
989
1032
|
wrap.appendChild(body);
|
|
990
1033
|
return wrap;
|
|
991
1034
|
}
|
|
1035
|
+
function hostOf(url) {
|
|
1036
|
+
try {
|
|
1037
|
+
return new URL(url, window.location.href).host;
|
|
1038
|
+
} catch {
|
|
1039
|
+
return String(url).slice(0, 40);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
function selectPanel({ title, note, searchable, width, remote, value, load, reload, linkOf, onPick, emptyText }) {
|
|
1043
|
+
const wrap = panel(title);
|
|
1044
|
+
wrap.classList.add("ke-panel-wide", "ke-panel-select");
|
|
1045
|
+
if (width) wrap.style.width = `${Math.min(420, Math.max(200, Number(width)))}px`;
|
|
1046
|
+
const head = wrap.querySelector(".ke-panel-head");
|
|
1047
|
+
const countEl = h("span.ke-panel-note", { text: note || "" });
|
|
1048
|
+
if (remote) {
|
|
1049
|
+
head.appendChild(h("button.ke-panel-icon", {
|
|
1050
|
+
type: "button",
|
|
1051
|
+
title: "重新加载数据",
|
|
1052
|
+
html: icon("retry"),
|
|
1053
|
+
onclick: () => run(reload || load, true)
|
|
1054
|
+
}));
|
|
1055
|
+
}
|
|
1056
|
+
head.appendChild(countEl);
|
|
1057
|
+
const body = h("div.ke-sel-body");
|
|
1058
|
+
wrap.appendChild(body);
|
|
1059
|
+
let items = [];
|
|
1060
|
+
let keyword = "";
|
|
1061
|
+
let generation = 0;
|
|
1062
|
+
if (searchable) {
|
|
1063
|
+
const search = h("input.ke-input.ke-sel-search", {
|
|
1064
|
+
type: "search",
|
|
1065
|
+
placeholder: "搜索…",
|
|
1066
|
+
"data-k-autofocus": ""
|
|
1067
|
+
});
|
|
1068
|
+
search.addEventListener("input", () => {
|
|
1069
|
+
keyword = search.value.trim().toLowerCase();
|
|
1070
|
+
renderRows();
|
|
1071
|
+
});
|
|
1072
|
+
wrap.insertBefore(search, body);
|
|
1073
|
+
}
|
|
1074
|
+
function setState(kind, message) {
|
|
1075
|
+
body.textContent = "";
|
|
1076
|
+
body.dataset.state = kind;
|
|
1077
|
+
if (kind === "loading") {
|
|
1078
|
+
for (let i = 0; i < 3; i += 1) body.appendChild(h("div.ke-sel-skel"));
|
|
1079
|
+
body.insertBefore(h("div.ke-sel-loading", {}, [h("span.ke-spinner"), "数据加载中…"]), body.firstChild);
|
|
1080
|
+
} else if (kind === "error") {
|
|
1081
|
+
body.appendChild(
|
|
1082
|
+
h("div.ke-sel-tip", {}, [
|
|
1083
|
+
h("span.ke-sel-tip-text", { text: message || "数据加载失败" }),
|
|
1084
|
+
h("button.ke-btn-primary", { type: "button", text: "重试", onclick: () => run(reload || load, true) })
|
|
1085
|
+
])
|
|
1086
|
+
);
|
|
1087
|
+
} else {
|
|
1088
|
+
body.appendChild(h("div.ke-sel-tip", {}, [h("span.ke-sel-tip-text", { text: message || emptyText || "暂无数据" })]));
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
function renderRows() {
|
|
1092
|
+
const list = keyword ? items.filter((item) => `${item.label} ${item.desc} ${item.value} ${item.group}`.toLowerCase().includes(keyword)) : items;
|
|
1093
|
+
countEl.textContent = note || (keyword ? `${list.length} / ${items.length} 项` : `${items.length} 项`);
|
|
1094
|
+
body.textContent = "";
|
|
1095
|
+
delete body.dataset.state;
|
|
1096
|
+
if (!list.length) {
|
|
1097
|
+
setState("empty", keyword ? `没有匹配「${keyword}」的选项` : emptyText || "暂无数据");
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
let group = null;
|
|
1101
|
+
list.forEach((item) => {
|
|
1102
|
+
if (item.group && item.group !== group) {
|
|
1103
|
+
group = item.group;
|
|
1104
|
+
body.appendChild(h("div.ke-panel-sect", { text: group }));
|
|
1105
|
+
}
|
|
1106
|
+
body.appendChild(row(item));
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
function row(item) {
|
|
1110
|
+
const link = linkOf ? linkOf(item) : null;
|
|
1111
|
+
const el = h("button.ke-sel-row", {
|
|
1112
|
+
type: "button",
|
|
1113
|
+
disabled: item.disabled || void 0,
|
|
1114
|
+
dataset: { kValue: item.value },
|
|
1115
|
+
title: item.disabled ? "该项不可用" : link && link.mode === "open" ? `打开 ${link.url}` : link ? `插入链接:${link.url}` : item.title || "",
|
|
1116
|
+
onclick: () => {
|
|
1117
|
+
if (item.disabled) return;
|
|
1118
|
+
onPick(item);
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
el.appendChild(h("span.ke-sel-main", {}, [
|
|
1122
|
+
h("span.ke-sel-label", { text: item.label }),
|
|
1123
|
+
item.desc ? h("span.ke-sel-desc", { text: item.desc }) : null,
|
|
1124
|
+
link ? h("span.ke-sel-link", { html: `${icon("link")}<i>${hostOf(link.url)}</i>` }) : null
|
|
1125
|
+
]));
|
|
1126
|
+
if (value && String(value) === String(item.value)) {
|
|
1127
|
+
el.classList.add("ke-is-active");
|
|
1128
|
+
el.appendChild(h("span.ke-sel-check", { html: icon("check") }));
|
|
1129
|
+
}
|
|
1130
|
+
if (link) el.classList.add("ke-has-link");
|
|
1131
|
+
return el;
|
|
1132
|
+
}
|
|
1133
|
+
function run(task, force) {
|
|
1134
|
+
generation += 1;
|
|
1135
|
+
const mine = generation;
|
|
1136
|
+
setState("loading");
|
|
1137
|
+
Promise.resolve().then(() => task({ force })).then((result) => {
|
|
1138
|
+
if (mine !== generation || !wrap.isConnected) return;
|
|
1139
|
+
items = Array.isArray(result) ? result : [];
|
|
1140
|
+
if (!items.length) setState("empty");
|
|
1141
|
+
else renderRows();
|
|
1142
|
+
}).catch((error) => {
|
|
1143
|
+
if (mine !== generation || !wrap.isConnected) return;
|
|
1144
|
+
setState("error", error && error.message ? error.message : "数据加载失败");
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
run(load);
|
|
1148
|
+
return wrap;
|
|
1149
|
+
}
|
|
992
1150
|
function normalizeUrl(url) {
|
|
993
1151
|
const value = String(url || "").trim();
|
|
994
1152
|
if (!value) return "";
|
|
@@ -1466,6 +1624,476 @@ function currentWidthPercent(img, figureWidth) {
|
|
|
1466
1624
|
if (match) return parseFloat(match[1]);
|
|
1467
1625
|
return img.getBoundingClientRect().width / figureWidth * 100;
|
|
1468
1626
|
}
|
|
1627
|
+
const ALLOWED_TAGS = /* @__PURE__ */ new Set([
|
|
1628
|
+
"a",
|
|
1629
|
+
"abbr",
|
|
1630
|
+
"b",
|
|
1631
|
+
"blockquote",
|
|
1632
|
+
"br",
|
|
1633
|
+
"code",
|
|
1634
|
+
"col",
|
|
1635
|
+
"colgroup",
|
|
1636
|
+
"dd",
|
|
1637
|
+
"del",
|
|
1638
|
+
"div",
|
|
1639
|
+
"dl",
|
|
1640
|
+
"dt",
|
|
1641
|
+
"em",
|
|
1642
|
+
"figcaption",
|
|
1643
|
+
"figure",
|
|
1644
|
+
"font",
|
|
1645
|
+
"h1",
|
|
1646
|
+
"h2",
|
|
1647
|
+
"h3",
|
|
1648
|
+
"h4",
|
|
1649
|
+
"h5",
|
|
1650
|
+
"h6",
|
|
1651
|
+
"hr",
|
|
1652
|
+
"i",
|
|
1653
|
+
"img",
|
|
1654
|
+
"ins",
|
|
1655
|
+
"kbd",
|
|
1656
|
+
"li",
|
|
1657
|
+
"mark",
|
|
1658
|
+
"ol",
|
|
1659
|
+
"p",
|
|
1660
|
+
"pre",
|
|
1661
|
+
"s",
|
|
1662
|
+
"samp",
|
|
1663
|
+
"section",
|
|
1664
|
+
"small",
|
|
1665
|
+
"span",
|
|
1666
|
+
"strong",
|
|
1667
|
+
"sub",
|
|
1668
|
+
"sup",
|
|
1669
|
+
"table",
|
|
1670
|
+
"tbody",
|
|
1671
|
+
"td",
|
|
1672
|
+
"tfoot",
|
|
1673
|
+
"th",
|
|
1674
|
+
"thead",
|
|
1675
|
+
"tr",
|
|
1676
|
+
"u",
|
|
1677
|
+
"ul",
|
|
1678
|
+
"video",
|
|
1679
|
+
"source"
|
|
1680
|
+
]);
|
|
1681
|
+
const GLOBAL_ATTRS = /* @__PURE__ */ new Set(["style", "class"]);
|
|
1682
|
+
const TAG_ATTRS = {
|
|
1683
|
+
A: ["href", "title", "target", "rel"],
|
|
1684
|
+
IMG: ["src", "alt", "title", "width", "height"],
|
|
1685
|
+
VIDEO: ["src", "poster", "controls", "loop", "muted", "playsinline", "preload", "width", "height"],
|
|
1686
|
+
SOURCE: ["src", "type"],
|
|
1687
|
+
FONT: ["size", "color"],
|
|
1688
|
+
TD: ["colspan", "rowspan"],
|
|
1689
|
+
TH: ["colspan", "rowspan"],
|
|
1690
|
+
OL: ["start"],
|
|
1691
|
+
LI: ["value"]
|
|
1692
|
+
};
|
|
1693
|
+
const DATA_PREFIX = ["data-k-", "data-keditor-"];
|
|
1694
|
+
const SAFE_URL_RE = /^(?:https?:|mailto:|tel:|blob:|\/|#|\.{1,2}\/|[^:]*$)/i;
|
|
1695
|
+
const SAFE_DATA_RE = /^data:image\/(?:png|jpe?g|gif|webp|svg\+xml);/i;
|
|
1696
|
+
function isSafeUrl(url) {
|
|
1697
|
+
if (!url) return false;
|
|
1698
|
+
const value = String(url).trim().replace(/\s+/g, "");
|
|
1699
|
+
if (/^(javascript|vbscript|file|about):/i.test(value)) return false;
|
|
1700
|
+
if (value.startsWith("data:")) return SAFE_DATA_RE.test(value);
|
|
1701
|
+
return SAFE_URL_RE.test(value);
|
|
1702
|
+
}
|
|
1703
|
+
function sanitizeStyle(value) {
|
|
1704
|
+
const out = [];
|
|
1705
|
+
for (const decl of String(value).split(";")) {
|
|
1706
|
+
const idx = decl.indexOf(":");
|
|
1707
|
+
if (idx < 0) continue;
|
|
1708
|
+
const prop = decl.slice(0, idx).trim().toLowerCase();
|
|
1709
|
+
const val = decl.slice(idx + 1).trim();
|
|
1710
|
+
if (!prop || !val) continue;
|
|
1711
|
+
if (!/^[a-z-]+$/.test(prop)) continue;
|
|
1712
|
+
if (prop.startsWith("mso-") || prop.startsWith("-mso-")) continue;
|
|
1713
|
+
if (/expression|javascript:|behavior:|url\(\s*['"]?\s*(javascript|data):/i.test(val)) continue;
|
|
1714
|
+
out.push(`${prop}:${val}`);
|
|
1715
|
+
}
|
|
1716
|
+
return out.join(";");
|
|
1717
|
+
}
|
|
1718
|
+
function sanitizeClass(value) {
|
|
1719
|
+
return String(value).split(/\s+/).filter((name) => /^[\w-]+$/.test(name) && !/^(script|javascript)/i.test(name)).join(" ");
|
|
1720
|
+
}
|
|
1721
|
+
function keepAttribute(tagName, attrName, attrValue) {
|
|
1722
|
+
const name = attrName.toLowerCase();
|
|
1723
|
+
if (name.startsWith("on")) return false;
|
|
1724
|
+
if (GLOBAL_ATTRS.has(name)) return true;
|
|
1725
|
+
if (DATA_PREFIX.some((prefix) => name.startsWith(prefix))) return true;
|
|
1726
|
+
const list = TAG_ATTRS[tagName] || [];
|
|
1727
|
+
return list.includes(name);
|
|
1728
|
+
}
|
|
1729
|
+
function sanitizeHtml(html, { allowEmpty = false } = {}) {
|
|
1730
|
+
if (!html) return allowEmpty ? "" : "<p><br></p>";
|
|
1731
|
+
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
1732
|
+
const root = doc.body.firstChild;
|
|
1733
|
+
walk(root);
|
|
1734
|
+
const result = root.innerHTML.trim();
|
|
1735
|
+
if (!result && !allowEmpty) return "<p><br></p>";
|
|
1736
|
+
return result;
|
|
1737
|
+
}
|
|
1738
|
+
function walk(node) {
|
|
1739
|
+
const children = Array.from(node.childNodes);
|
|
1740
|
+
for (const child of children) {
|
|
1741
|
+
if (child.nodeType === 8) {
|
|
1742
|
+
child.remove();
|
|
1743
|
+
continue;
|
|
1744
|
+
}
|
|
1745
|
+
if (child.nodeType === 3) continue;
|
|
1746
|
+
if (child.nodeType !== 1) {
|
|
1747
|
+
child.remove();
|
|
1748
|
+
continue;
|
|
1749
|
+
}
|
|
1750
|
+
const tagName = child.tagName;
|
|
1751
|
+
if (!ALLOWED_TAGS.has(tagName.toLowerCase())) {
|
|
1752
|
+
if (tagName === "SCRIPT" || tagName === "STYLE" || tagName === "IFRAME" || tagName === "OBJECT" || tagName === "EMBED" || tagName === "FORM" || tagName === "LINK" || tagName === "META") {
|
|
1753
|
+
child.remove();
|
|
1754
|
+
continue;
|
|
1755
|
+
}
|
|
1756
|
+
const parent = child.parentNode;
|
|
1757
|
+
while (child.firstChild) parent.insertBefore(child.firstChild, child);
|
|
1758
|
+
parent.removeChild(child);
|
|
1759
|
+
continue;
|
|
1760
|
+
}
|
|
1761
|
+
for (const attr of Array.from(child.attributes)) {
|
|
1762
|
+
if (!keepAttribute(tagName, attr.name, attr.value)) {
|
|
1763
|
+
child.removeAttribute(attr.name);
|
|
1764
|
+
continue;
|
|
1765
|
+
}
|
|
1766
|
+
const lower = attr.name.toLowerCase();
|
|
1767
|
+
if (lower === "style") child.setAttribute("style", sanitizeStyle(attr.value));
|
|
1768
|
+
else if (lower === "class") {
|
|
1769
|
+
const cls = sanitizeClass(attr.value);
|
|
1770
|
+
if (cls) child.setAttribute("class", cls);
|
|
1771
|
+
else child.removeAttribute("class");
|
|
1772
|
+
} else if (lower === "href" || lower === "src" || lower === "poster") {
|
|
1773
|
+
if (!isSafeUrl(attr.value)) child.removeAttribute(attr.name);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
if (tagName === "A" && child.getAttribute("target") === "_blank") child.setAttribute("rel", "noopener noreferrer");
|
|
1777
|
+
if (child.getAttribute("href") === "" || !child.getAttribute("href")) {
|
|
1778
|
+
if (tagName === "A") child.removeAttribute("href");
|
|
1779
|
+
}
|
|
1780
|
+
walk(child);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
const WORD_FONT_SIZES = { 1: 10, 2: 13, 3: 16, 4: 18, 5: 24, 6: 32, 7: 48 };
|
|
1784
|
+
function isWordHtml(html) {
|
|
1785
|
+
return /urn:schemas-microsoft-com|class=["']?Mso|ProgID|w:val=/i.test(html);
|
|
1786
|
+
}
|
|
1787
|
+
function cleanWordHtml(html) {
|
|
1788
|
+
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
1789
|
+
const root = doc.body.firstChild;
|
|
1790
|
+
root.querySelectorAll("*").forEach((node) => {
|
|
1791
|
+
const tag = node.tagName.toLowerCase();
|
|
1792
|
+
if (tag.startsWith("o:") || tag.startsWith("w:") || tag === "style" || tag === "script" || tag === "link" || tag === "meta") {
|
|
1793
|
+
node.remove();
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
if (tag === "img") {
|
|
1797
|
+
const width = Number(node.getAttribute("width") || 0);
|
|
1798
|
+
const height = Number(node.getAttribute("height") || 0);
|
|
1799
|
+
if (width && width <= 3 || height && height <= 3) node.remove();
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
if (tag === "font") {
|
|
1803
|
+
const size = WORD_FONT_SIZES[Number(node.getAttribute("size"))];
|
|
1804
|
+
const span = doc.createElement("span");
|
|
1805
|
+
const styles = [];
|
|
1806
|
+
if (size) styles.push(`font-size:${size}px`);
|
|
1807
|
+
if (node.color) styles.push(`color:${node.color}`);
|
|
1808
|
+
if (styles.length) span.setAttribute("style", styles.join(";"));
|
|
1809
|
+
while (node.firstChild) span.appendChild(node.firstChild);
|
|
1810
|
+
node.replaceWith(span);
|
|
1811
|
+
return;
|
|
1812
|
+
}
|
|
1813
|
+
for (const attr of Array.from(node.attributes)) {
|
|
1814
|
+
const name = attr.name.toLowerCase();
|
|
1815
|
+
if (name.startsWith("mso-") || name === "class" || name.startsWith("_") || name.startsWith("w:") || name.startsWith("o:") || name === "lang" || name === "dir") {
|
|
1816
|
+
node.removeAttribute(attr.name);
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
});
|
|
1820
|
+
return root.innerHTML;
|
|
1821
|
+
}
|
|
1822
|
+
function escapeHtml(text) {
|
|
1823
|
+
return String(text).replace(/[&<>"']/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[ch]);
|
|
1824
|
+
}
|
|
1825
|
+
function toPlainText(html) {
|
|
1826
|
+
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
1827
|
+
doc.body.querySelectorAll("style,script").forEach((n) => n.remove());
|
|
1828
|
+
doc.body.innerHTML = doc.body.innerHTML.replace(/<\/(p|div|h[1-6]|li|blockquote|pre|tr)>/gi, "\n");
|
|
1829
|
+
return doc.body.textContent.replace(/\u00a0/g, " ").replace(/\n{3,}/g, "\n\n").trim();
|
|
1830
|
+
}
|
|
1831
|
+
const DEFAULT_TIMEOUT = 1e4;
|
|
1832
|
+
function pickArray(raw) {
|
|
1833
|
+
if (Array.isArray(raw)) return raw;
|
|
1834
|
+
if (!raw || typeof raw !== "object") return [];
|
|
1835
|
+
for (const key of ["items", "list", "data", "rows", "records", "result"]) {
|
|
1836
|
+
const value = raw[key];
|
|
1837
|
+
if (Array.isArray(value)) return value;
|
|
1838
|
+
if (value && typeof value === "object") {
|
|
1839
|
+
const nested = pickArray(value);
|
|
1840
|
+
if (nested.length) return nested;
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
return [];
|
|
1844
|
+
}
|
|
1845
|
+
function first(entry, keys) {
|
|
1846
|
+
for (const key of keys) {
|
|
1847
|
+
const value = entry[key];
|
|
1848
|
+
if (value !== void 0 && value !== null) return value;
|
|
1849
|
+
}
|
|
1850
|
+
return void 0;
|
|
1851
|
+
}
|
|
1852
|
+
function normalizeItem(entry, index) {
|
|
1853
|
+
if (entry === void 0 || entry === null) return null;
|
|
1854
|
+
if (typeof entry === "string" || typeof entry === "number") {
|
|
1855
|
+
const text = String(entry);
|
|
1856
|
+
return { value: text, label: text, desc: "", url: "", group: "", html: "", disabled: false, raw: entry };
|
|
1857
|
+
}
|
|
1858
|
+
if (typeof entry !== "object") return null;
|
|
1859
|
+
const value = first(entry, ["value", "id", "key", "val", "code"]);
|
|
1860
|
+
const label = first(entry, ["label", "text", "name", "title"]);
|
|
1861
|
+
return {
|
|
1862
|
+
value: value === void 0 ? String(index) : String(value),
|
|
1863
|
+
label: label === void 0 || label === "" ? value === void 0 ? String(index) : String(value) : String(label),
|
|
1864
|
+
desc: String(first(entry, ["desc", "description", "sub", "subtitle", "tip"]) ?? ""),
|
|
1865
|
+
url: String(first(entry, ["url", "href", "link", "jumpUrl", "jump"]) ?? ""),
|
|
1866
|
+
group: String(entry.group ?? ""),
|
|
1867
|
+
html: String(entry.html ?? ""),
|
|
1868
|
+
disabled: !!entry.disabled,
|
|
1869
|
+
raw: entry
|
|
1870
|
+
};
|
|
1871
|
+
}
|
|
1872
|
+
function normalizeItems(raw) {
|
|
1873
|
+
return pickArray(raw).map(normalizeItem).filter(Boolean);
|
|
1874
|
+
}
|
|
1875
|
+
function normalizePosition(spec = {}) {
|
|
1876
|
+
const raw = spec.position !== void 0 ? spec.position : spec.at;
|
|
1877
|
+
const num = (value) => Math.max(0, Math.floor(Number(value)));
|
|
1878
|
+
const named = (value) => typeof value === "object" && value ? value.before || value.after : value;
|
|
1879
|
+
if (typeof raw === "number" && Number.isFinite(raw)) return num(raw);
|
|
1880
|
+
if (typeof raw === "string" && raw) {
|
|
1881
|
+
const key = raw.toLowerCase();
|
|
1882
|
+
if (key === "start" || key === "begin" || key === "first") return "start";
|
|
1883
|
+
if (key === "end" || key === "last" || key === "main") return "end";
|
|
1884
|
+
if (key === "tail" || key === ":tail" || key === "right") return "tail";
|
|
1885
|
+
return { before: raw };
|
|
1886
|
+
}
|
|
1887
|
+
if (raw && typeof raw === "object") {
|
|
1888
|
+
if (raw.before) return { before: String(named(raw)) };
|
|
1889
|
+
if (raw.after) return { after: String(named(raw)) };
|
|
1890
|
+
}
|
|
1891
|
+
if (typeof spec.index === "number" && Number.isFinite(spec.index)) return num(spec.index);
|
|
1892
|
+
if (spec.before) return { before: String(spec.before) };
|
|
1893
|
+
if (spec.after) return { after: String(spec.after) };
|
|
1894
|
+
return null;
|
|
1895
|
+
}
|
|
1896
|
+
function normalizeCustomButton(spec) {
|
|
1897
|
+
if (!spec || typeof spec !== "object") return null;
|
|
1898
|
+
const name = String(spec.name || "").trim();
|
|
1899
|
+
if (!name) return null;
|
|
1900
|
+
return {
|
|
1901
|
+
name,
|
|
1902
|
+
/** 在工具栏里的位置(null = 默认追加到主区末尾) */
|
|
1903
|
+
position: normalizePosition(spec),
|
|
1904
|
+
type: spec.type === "button" ? "button" : "select",
|
|
1905
|
+
title: spec.title || spec.text || name,
|
|
1906
|
+
text: spec.text || "",
|
|
1907
|
+
placeholder: spec.placeholder || spec.text || "请选择",
|
|
1908
|
+
icon: spec.icon || "",
|
|
1909
|
+
width: Number(spec.width) > 0 ? Number(spec.width) : 0,
|
|
1910
|
+
searchable: !!spec.searchable,
|
|
1911
|
+
note: spec.note ? String(spec.note) : "",
|
|
1912
|
+
emptyText: spec.emptyText ? String(spec.emptyText) : "",
|
|
1913
|
+
cache: spec.cache !== false,
|
|
1914
|
+
keepValue: spec.keepValue !== false,
|
|
1915
|
+
multiple: false,
|
|
1916
|
+
options: spec.options ?? null,
|
|
1917
|
+
load: typeof spec.load === "function" ? spec.load : null,
|
|
1918
|
+
url: spec.url ? String(spec.url) : "",
|
|
1919
|
+
fetchOptions: spec.fetchOptions && typeof spec.fetchOptions === "object" ? spec.fetchOptions : {},
|
|
1920
|
+
pick: typeof spec.pick === "function" ? spec.pick : null,
|
|
1921
|
+
link: spec.link ?? null,
|
|
1922
|
+
insert: spec.insert === void 0 ? "auto" : spec.insert,
|
|
1923
|
+
onSelect: typeof spec.onSelect === "function" ? spec.onSelect : null,
|
|
1924
|
+
onClick: typeof spec.onClick === "function" ? spec.onClick : null,
|
|
1925
|
+
active: typeof spec.active === "function" ? spec.active : null,
|
|
1926
|
+
enabled: typeof spec.enabled === "function" ? spec.enabled : null
|
|
1927
|
+
};
|
|
1928
|
+
}
|
|
1929
|
+
function normalizeCustomButtons(list) {
|
|
1930
|
+
if (!Array.isArray(list)) return [];
|
|
1931
|
+
return list.map(normalizeCustomButton).filter(Boolean);
|
|
1932
|
+
}
|
|
1933
|
+
function createButtonRuntime() {
|
|
1934
|
+
return { items: null, loaded: false, loading: null, value: null, controller: null, error: "", gen: 0, external: false };
|
|
1935
|
+
}
|
|
1936
|
+
async function fetchItems(spec, editor, rt) {
|
|
1937
|
+
if (spec.load) return normalizeItems(await spec.load(editor));
|
|
1938
|
+
if (spec.url) return normalizeItems(await httpFetch(spec, editor, rt));
|
|
1939
|
+
return normalizeItems(spec.options);
|
|
1940
|
+
}
|
|
1941
|
+
async function httpFetch(spec, editor, rt) {
|
|
1942
|
+
var _a;
|
|
1943
|
+
const cfg = spec.fetchOptions;
|
|
1944
|
+
const impl = cfg.fetch || (typeof fetch === "function" ? fetch.bind(globalThis) : null);
|
|
1945
|
+
if (!impl) throw new Error("当前环境无 fetch,请改用 load 函数提供数据");
|
|
1946
|
+
(_a = rt.controller) == null ? void 0 : _a.abort();
|
|
1947
|
+
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
1948
|
+
rt.controller = controller;
|
|
1949
|
+
const timeout = Number(cfg.timeout) > 0 ? Number(cfg.timeout) : DEFAULT_TIMEOUT;
|
|
1950
|
+
const timer = setTimeout(() => controller == null ? void 0 : controller.abort(), timeout);
|
|
1951
|
+
try {
|
|
1952
|
+
const res = await impl(spec.url, {
|
|
1953
|
+
method: cfg.method || "GET",
|
|
1954
|
+
headers: cfg.headers,
|
|
1955
|
+
body: cfg.body,
|
|
1956
|
+
credentials: cfg.credentials,
|
|
1957
|
+
signal: controller ? controller.signal : void 0
|
|
1958
|
+
});
|
|
1959
|
+
if (!res || res.ok === false) throw new Error(`接口返回 ${res && res.status ? res.status : "异常"}`);
|
|
1960
|
+
const payload = cfg.parser ? await cfg.parser(res) : await res.json();
|
|
1961
|
+
return spec.pick ? spec.pick(payload, editor) : payload;
|
|
1962
|
+
} catch (error) {
|
|
1963
|
+
if (error && error.name === "AbortError") throw new Error(`请求超时(${timeout}ms)或被取消`);
|
|
1964
|
+
throw error;
|
|
1965
|
+
} finally {
|
|
1966
|
+
clearTimeout(timer);
|
|
1967
|
+
if (rt.controller === controller) rt.controller = null;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
function loadItems(spec, editor, rt, { force = false } = {}) {
|
|
1971
|
+
if (!force && rt.external && rt.items) return Promise.resolve(rt.items);
|
|
1972
|
+
if (!force && spec.cache && rt.items) return Promise.resolve(rt.items);
|
|
1973
|
+
if (!force && rt.loading) return rt.loading;
|
|
1974
|
+
const gen = rt.gen;
|
|
1975
|
+
const task = fetchItems(spec, editor, rt).then((items) => {
|
|
1976
|
+
if (gen !== rt.gen) return rt.items;
|
|
1977
|
+
rt.items = items;
|
|
1978
|
+
rt.loaded = true;
|
|
1979
|
+
rt.error = "";
|
|
1980
|
+
return items;
|
|
1981
|
+
}).catch((error) => {
|
|
1982
|
+
if (gen !== rt.gen) return rt.items;
|
|
1983
|
+
rt.error = error && error.message ? error.message : String(error);
|
|
1984
|
+
throw error;
|
|
1985
|
+
}).finally(() => {
|
|
1986
|
+
if (rt.loading === task) rt.loading = null;
|
|
1987
|
+
});
|
|
1988
|
+
rt.loading = task;
|
|
1989
|
+
return task;
|
|
1990
|
+
}
|
|
1991
|
+
function setRuntimeItems(rt, raw) {
|
|
1992
|
+
rt.gen += 1;
|
|
1993
|
+
rt.items = normalizeItems(raw);
|
|
1994
|
+
rt.loaded = true;
|
|
1995
|
+
rt.error = "";
|
|
1996
|
+
rt.external = true;
|
|
1997
|
+
abortRuntime(rt);
|
|
1998
|
+
rt.loading = null;
|
|
1999
|
+
return rt.items;
|
|
2000
|
+
}
|
|
2001
|
+
function abortRuntime(rt) {
|
|
2002
|
+
var _a;
|
|
2003
|
+
(_a = rt.controller) == null ? void 0 : _a.abort();
|
|
2004
|
+
rt.controller = null;
|
|
2005
|
+
}
|
|
2006
|
+
function fill(template, item) {
|
|
2007
|
+
return String(template).replace(/\{(\w+)\}/g, (match, key) => {
|
|
2008
|
+
if (item[key] !== void 0 && item[key] !== null) return String(item[key]);
|
|
2009
|
+
const fromRaw = item.raw && item.raw[key];
|
|
2010
|
+
return fromRaw === void 0 || fromRaw === null ? match : String(fromRaw);
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
function pick$1(raw, item, editor) {
|
|
2014
|
+
return typeof raw === "function" ? raw(item, editor) : raw;
|
|
2015
|
+
}
|
|
2016
|
+
function resolveLink(spec, item, editor) {
|
|
2017
|
+
const conf = spec.link;
|
|
2018
|
+
let raw = null;
|
|
2019
|
+
if (typeof conf === "function") raw = conf(item, editor);
|
|
2020
|
+
else if (typeof conf === "string") raw = { url: conf };
|
|
2021
|
+
else if (conf && typeof conf === "object") raw = conf;
|
|
2022
|
+
else if (conf === true || item.url) raw = { url: item.url };
|
|
2023
|
+
if (!raw) return null;
|
|
2024
|
+
if (typeof raw === "string") raw = { url: raw };
|
|
2025
|
+
const url = normalizeUrl(fill(String(pick$1(raw.url, item, editor) ?? ""), item));
|
|
2026
|
+
if (!url || !isSafeUrl(url)) return null;
|
|
2027
|
+
const text = pick$1(raw.text, item, editor);
|
|
2028
|
+
const title = pick$1(raw.title, item, editor);
|
|
2029
|
+
const mode = raw.mode === "open" ? "open" : "insert";
|
|
2030
|
+
const target = raw.target === "_self" || raw.target === false ? "_self" : "_blank";
|
|
2031
|
+
return {
|
|
2032
|
+
mode,
|
|
2033
|
+
url,
|
|
2034
|
+
text: text === void 0 || text === null || text === "" ? item.label : fill(String(text), item),
|
|
2035
|
+
target,
|
|
2036
|
+
title: title ? fill(String(title), item) : ""
|
|
2037
|
+
};
|
|
2038
|
+
}
|
|
2039
|
+
function insertText(editor, text) {
|
|
2040
|
+
editor.commands.insertHTML(escapeHtml(text));
|
|
2041
|
+
}
|
|
2042
|
+
async function applySelection(editor, spec, rt, item) {
|
|
2043
|
+
editor.restoreSelection();
|
|
2044
|
+
const link = resolveLink(spec, item, editor);
|
|
2045
|
+
const ctx = {
|
|
2046
|
+
editor,
|
|
2047
|
+
item,
|
|
2048
|
+
link,
|
|
2049
|
+
insertLink: (override) => insertLink(editor, { ...link, ...override }),
|
|
2050
|
+
insertHtml: (html) => editor.commands.insertHTML(String(html)),
|
|
2051
|
+
insertText: (text) => insertText(editor, text),
|
|
2052
|
+
open: (url, target) => openUrl(url || link && link.url, target || link && link.target)
|
|
2053
|
+
};
|
|
2054
|
+
let action = "none";
|
|
2055
|
+
let handled = false;
|
|
2056
|
+
if (spec.onSelect) {
|
|
2057
|
+
handled = await spec.onSelect(item, ctx) === false;
|
|
2058
|
+
}
|
|
2059
|
+
if (!handled) action = doDefaultInsert(editor, spec, item, link);
|
|
2060
|
+
if (spec.keepValue) rt.value = item;
|
|
2061
|
+
const payload = { name: spec.name, item, link, action, editor };
|
|
2062
|
+
editor.emit("customselect", payload);
|
|
2063
|
+
return payload;
|
|
2064
|
+
}
|
|
2065
|
+
function doDefaultInsert(editor, spec, item, link) {
|
|
2066
|
+
const mode = spec.insert;
|
|
2067
|
+
if (mode === false || mode === "none") return "none";
|
|
2068
|
+
if (link && link.mode === "open") {
|
|
2069
|
+
openUrl(link.url, link.target);
|
|
2070
|
+
return "open";
|
|
2071
|
+
}
|
|
2072
|
+
if (mode === "link" || mode === "auto" && link) {
|
|
2073
|
+
if (!link) return "none";
|
|
2074
|
+
insertLink(editor, link);
|
|
2075
|
+
return "link";
|
|
2076
|
+
}
|
|
2077
|
+
if (mode === "html" || mode === "auto" && item.html) {
|
|
2078
|
+
if (!item.html) return "none";
|
|
2079
|
+
editor.commands.insertHTML(item.html);
|
|
2080
|
+
return "html";
|
|
2081
|
+
}
|
|
2082
|
+
insertText(editor, item.label);
|
|
2083
|
+
return "text";
|
|
2084
|
+
}
|
|
2085
|
+
function insertLink(editor, link) {
|
|
2086
|
+
editor.commands.createLink({
|
|
2087
|
+
url: link.url,
|
|
2088
|
+
text: link.text,
|
|
2089
|
+
target: link.target !== "_self"
|
|
2090
|
+
});
|
|
2091
|
+
}
|
|
2092
|
+
function openUrl(url, target = "_blank") {
|
|
2093
|
+
if (!url || !isSafeUrl(url)) return;
|
|
2094
|
+
if (target === "_self") window.location.href = url;
|
|
2095
|
+
else window.open(url, "_blank", "noopener,noreferrer");
|
|
2096
|
+
}
|
|
1469
2097
|
const CARET = '<svg class="ke-caret" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>';
|
|
1470
2098
|
const SCROLL_HINT_KEY = "keditor.toolbarScrollHint";
|
|
1471
2099
|
const SCROLL_HINT_ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round"><path d="M8 7 4 12l4 5"/><path d="M16 7l4 5-4 5"/><path d="M4 12h16"/></svg>';
|
|
@@ -1509,7 +2137,6 @@ const DEFAULT_TOOLBAR = [
|
|
|
1509
2137
|
"clear",
|
|
1510
2138
|
":tail",
|
|
1511
2139
|
"source",
|
|
1512
|
-
"preview",
|
|
1513
2140
|
"fullscreen"
|
|
1514
2141
|
];
|
|
1515
2142
|
const ITEMS = {
|
|
@@ -1535,33 +2162,43 @@ const ITEMS = {
|
|
|
1535
2162
|
clear: { icon: "clear", title: "清除格式", run: (e) => e.commands.clearFormat() },
|
|
1536
2163
|
source: { icon: "source", title: "查看 / 编辑 HTML 源码(⌘/Ctrl+Shift+K)", text: "源码", run: (e) => e.toggleSource(), active: (s) => s.source },
|
|
1537
2164
|
preview: { icon: "preview", title: "预览效果(手机 / 平板 / 电脑宽度)", text: "预览", run: (e) => e.togglePreview(), active: (s) => s.preview },
|
|
2165
|
+
// 默认工具栏已不包含,可用 toolbar / position 显式摆放
|
|
1538
2166
|
fullscreen: { icon: "expand", title: "全屏编辑", text: "全屏", run: (e) => e.toggleFullscreen(), active: (s) => s.fullscreen },
|
|
1539
2167
|
foreColor: { pop: "fore", title: "字体颜色", toggles: true, swatch: "fore" },
|
|
1540
2168
|
backColor: { pop: "back", title: "背景高亮色", toggles: true, swatch: "back" }
|
|
1541
2169
|
};
|
|
1542
2170
|
class Toolbar {
|
|
1543
|
-
constructor(editor, items = DEFAULT_TOOLBAR) {
|
|
2171
|
+
constructor(editor, items = DEFAULT_TOOLBAR, customButtons = []) {
|
|
1544
2172
|
this.editor = editor;
|
|
1545
2173
|
this.buttons = /* @__PURE__ */ new Map();
|
|
2174
|
+
this.custom = /* @__PURE__ */ new Map();
|
|
1546
2175
|
this.colorState = { foreColor: "#3f3f3f", backColor: "#f6c344" };
|
|
1547
2176
|
this.recent = { foreColor: [], backColor: [] };
|
|
1548
2177
|
this.el = editor.tbEl;
|
|
2178
|
+
for (const spec of customButtons) this.registerCustom(spec);
|
|
1549
2179
|
this.build(items);
|
|
1550
2180
|
this.setupScrollHints();
|
|
1551
2181
|
}
|
|
1552
2182
|
build(items) {
|
|
1553
2183
|
const group = h("div.ke-tb-group");
|
|
1554
2184
|
this.el.appendChild(group);
|
|
1555
|
-
|
|
1556
|
-
|
|
2185
|
+
this.groupEl = group;
|
|
2186
|
+
this.tailEl = null;
|
|
2187
|
+
const target = () => this.tailEl || group;
|
|
1557
2188
|
for (const key of items) {
|
|
1558
2189
|
if (key === "|") {
|
|
1559
|
-
|
|
2190
|
+
target().appendChild(h("span.ke-tb-sep"));
|
|
1560
2191
|
continue;
|
|
1561
2192
|
}
|
|
1562
2193
|
if (key === ":tail") {
|
|
1563
|
-
|
|
1564
|
-
this.el.appendChild(
|
|
2194
|
+
this.tailEl = h("div.ke-tb-tail");
|
|
2195
|
+
this.el.appendChild(this.tailEl);
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
if (this.custom.has(key)) {
|
|
2199
|
+
const entry = this.custom.get(key);
|
|
2200
|
+
entry.button = this.buildCustomButton(entry);
|
|
2201
|
+
target().appendChild(entry.button);
|
|
1565
2202
|
continue;
|
|
1566
2203
|
}
|
|
1567
2204
|
if (key === "heading") {
|
|
@@ -1576,6 +2213,11 @@ class Toolbar {
|
|
|
1576
2213
|
if (!spec) continue;
|
|
1577
2214
|
target().appendChild(this.buildButton(key, spec));
|
|
1578
2215
|
}
|
|
2216
|
+
for (const [name, entry] of this.custom) {
|
|
2217
|
+
if (entry.button) continue;
|
|
2218
|
+
entry.button = this.buildCustomButton(entry);
|
|
2219
|
+
this.insertButton(entry.button, entry.spec.position);
|
|
2220
|
+
}
|
|
1579
2221
|
}
|
|
1580
2222
|
buildButton(name, spec) {
|
|
1581
2223
|
const button = h("button.ke-btn", {
|
|
@@ -1681,11 +2323,220 @@ class Toolbar {
|
|
|
1681
2323
|
}));
|
|
1682
2324
|
}
|
|
1683
2325
|
});
|
|
1684
|
-
button.innerHTML = `<span class="ke-btn-text">字号</span>${CARET}`;
|
|
1685
|
-
this.fontSizeEl = button;
|
|
1686
|
-
this.buttons.set("fontsize", button);
|
|
2326
|
+
button.innerHTML = `<span class="ke-btn-text">字号</span>${CARET}`;
|
|
2327
|
+
this.fontSizeEl = button;
|
|
2328
|
+
this.buttons.set("fontsize", button);
|
|
2329
|
+
return button;
|
|
2330
|
+
}
|
|
2331
|
+
/* ---------------- 自定义按钮 ---------------- */
|
|
2332
|
+
/** 登记一个自定义按钮规格;统一在此规范化,保证配置与 addButton 两条路径行为一致 */
|
|
2333
|
+
registerCustom(raw) {
|
|
2334
|
+
const spec = normalizeCustomButton(raw);
|
|
2335
|
+
if (!spec) return null;
|
|
2336
|
+
const prev = this.custom.get(spec.name);
|
|
2337
|
+
const entry = { spec, rt: prev ? prev.rt : createButtonRuntime(), button: prev ? prev.button : null };
|
|
2338
|
+
this.custom.set(spec.name, entry);
|
|
2339
|
+
return entry;
|
|
2340
|
+
}
|
|
2341
|
+
buildCustomButton(entry) {
|
|
2342
|
+
const { spec } = entry;
|
|
2343
|
+
const isSelect = spec.type === "select";
|
|
2344
|
+
const button = h("button.ke-btn", {
|
|
2345
|
+
type: "button",
|
|
2346
|
+
class: isSelect ? "ke-btn-label ke-btn-custom" : "ke-btn-custom",
|
|
2347
|
+
title: spec.title,
|
|
2348
|
+
"data-k-item": spec.name,
|
|
2349
|
+
dataset: { kPop: spec.name, kCustom: spec.name },
|
|
2350
|
+
onclick: (event) => {
|
|
2351
|
+
event.preventDefault();
|
|
2352
|
+
if (isSelect) this.openCustom(spec.name);
|
|
2353
|
+
else this.runCustomButton(spec);
|
|
2354
|
+
}
|
|
2355
|
+
});
|
|
2356
|
+
if (spec.icon) button.innerHTML = /</.test(spec.icon) ? spec.icon : icon(spec.icon);
|
|
2357
|
+
if (isSelect) {
|
|
2358
|
+
button.appendChild(h("span.ke-btn-text", { text: spec.text || spec.placeholder }));
|
|
2359
|
+
button.insertAdjacentHTML("beforeend", CARET);
|
|
2360
|
+
} else if (spec.text) {
|
|
2361
|
+
button.appendChild(h("span.ke-btn-text", { text: spec.text }));
|
|
2362
|
+
}
|
|
2363
|
+
this.syncCustomLabel(spec.name, button);
|
|
1687
2364
|
return button;
|
|
1688
2365
|
}
|
|
2366
|
+
/** 按钮文案跟随当前选中项(与段落样式 / 字号按钮一致) */
|
|
2367
|
+
syncCustomLabel(name, node) {
|
|
2368
|
+
const entry = this.custom.get(name);
|
|
2369
|
+
const button = node || (entry == null ? void 0 : entry.button);
|
|
2370
|
+
if (!entry || !button) return;
|
|
2371
|
+
const { spec, rt } = entry;
|
|
2372
|
+
if (spec.type !== "select") return;
|
|
2373
|
+
const label = button.querySelector(".ke-btn-text");
|
|
2374
|
+
if (!label) return;
|
|
2375
|
+
const picked = rt.value;
|
|
2376
|
+
label.textContent = picked ? picked.label : spec.text || spec.placeholder;
|
|
2377
|
+
button.title = picked ? `${spec.title}:${picked.label}` : spec.title;
|
|
2378
|
+
button.classList.toggle("ke-has-value", !!picked);
|
|
2379
|
+
}
|
|
2380
|
+
/** 打开下拉面板:数据异步加载,带缓存 / 重试 / 搜索 */
|
|
2381
|
+
openCustom(name) {
|
|
2382
|
+
const entry = this.custom.get(name);
|
|
2383
|
+
if (!entry) return;
|
|
2384
|
+
this.revealButton(entry.button);
|
|
2385
|
+
const { spec, rt } = entry;
|
|
2386
|
+
const remote = !!(spec.load || spec.url);
|
|
2387
|
+
const fetcher = ({ force } = {}) => loadItems(spec, this.editor, rt, { force });
|
|
2388
|
+
this.togglePop(name, entry.button, () => selectPanel({
|
|
2389
|
+
title: spec.title,
|
|
2390
|
+
note: spec.note || "",
|
|
2391
|
+
searchable: spec.searchable,
|
|
2392
|
+
width: spec.width,
|
|
2393
|
+
remote,
|
|
2394
|
+
value: rt.value ? rt.value.value : "",
|
|
2395
|
+
emptyText: spec.emptyText,
|
|
2396
|
+
load: fetcher,
|
|
2397
|
+
reload: fetcher,
|
|
2398
|
+
linkOf: (item) => resolveLink(spec, item, this.editor),
|
|
2399
|
+
onPick: (item) => this.pickCustom(name, item)
|
|
2400
|
+
}));
|
|
2401
|
+
}
|
|
2402
|
+
async pickCustom(name, item) {
|
|
2403
|
+
const entry = this.custom.get(name);
|
|
2404
|
+
if (!entry) return;
|
|
2405
|
+
this.editor.popover.close();
|
|
2406
|
+
try {
|
|
2407
|
+
await applySelection(this.editor, entry.spec, entry.rt, item);
|
|
2408
|
+
} catch (error) {
|
|
2409
|
+
this.editor.reportError("custombutton", error);
|
|
2410
|
+
}
|
|
2411
|
+
this.syncCustomLabel(name);
|
|
2412
|
+
this.editor.refresh();
|
|
2413
|
+
}
|
|
2414
|
+
/** type:'button' 的简单自定义按钮 */
|
|
2415
|
+
async runCustomButton(spec) {
|
|
2416
|
+
this.editor.popover.close();
|
|
2417
|
+
try {
|
|
2418
|
+
if (spec.onClick) await spec.onClick({ editor: this.editor, name: spec.name });
|
|
2419
|
+
this.editor.emit("customclick", { name: spec.name, editor: this.editor });
|
|
2420
|
+
} catch (error) {
|
|
2421
|
+
this.editor.reportError("custombutton", error);
|
|
2422
|
+
}
|
|
2423
|
+
this.editor.refresh();
|
|
2424
|
+
}
|
|
2425
|
+
/** 运行时新增自定义按钮;位置取 options.at ?? spec.position(默认追加到主分组末尾) */
|
|
2426
|
+
addButton(spec, options = {}) {
|
|
2427
|
+
var _a;
|
|
2428
|
+
const entry = this.registerCustom(spec);
|
|
2429
|
+
if (!entry) return null;
|
|
2430
|
+
if (entry.button) entry.button.remove();
|
|
2431
|
+
entry.button = this.buildCustomButton(entry);
|
|
2432
|
+
const where = options.at !== void 0 ? options.at : normalizePosition(options) ?? entry.spec.position;
|
|
2433
|
+
this.insertButton(entry.button, where, { withSep: where == null });
|
|
2434
|
+
this.revealButton(entry.button);
|
|
2435
|
+
(_a = this.syncScrollHints) == null ? void 0 : _a.call(this);
|
|
2436
|
+
return entry.button;
|
|
2437
|
+
}
|
|
2438
|
+
/** 工具栏内可定位的节点(主区 → 尾区的视觉顺序,不含分隔符) */
|
|
2439
|
+
buttonNodes() {
|
|
2440
|
+
const list = [];
|
|
2441
|
+
for (const host of [this.groupEl, this.tailEl]) {
|
|
2442
|
+
if (!host) continue;
|
|
2443
|
+
for (const node of host.children) if (node.dataset && node.dataset.kItem) list.push(node);
|
|
2444
|
+
}
|
|
2445
|
+
return list;
|
|
2446
|
+
}
|
|
2447
|
+
/** 位置描述 → {parent, ref},ref 为 null 表示追加到 parent 末尾 */
|
|
2448
|
+
slotOf(where) {
|
|
2449
|
+
const mainEnd = { parent: this.groupEl || this.el, ref: null };
|
|
2450
|
+
if (where === "tail") return { parent: this.tailEl || this.groupEl || this.el, ref: null };
|
|
2451
|
+
if (where == null || where === "end") return mainEnd;
|
|
2452
|
+
if (where === "start") {
|
|
2453
|
+
const first2 = this.buttonNodes()[0];
|
|
2454
|
+
return first2 ? { parent: first2.parentElement, ref: first2 } : mainEnd;
|
|
2455
|
+
}
|
|
2456
|
+
const nodes = this.buttonNodes();
|
|
2457
|
+
if (typeof where === "number") {
|
|
2458
|
+
const ref = nodes[Math.max(0, Math.floor(where))];
|
|
2459
|
+
if (ref) return { parent: ref.parentElement, ref };
|
|
2460
|
+
const last = nodes[nodes.length - 1];
|
|
2461
|
+
return last ? { parent: last.parentElement, ref: null } : mainEnd;
|
|
2462
|
+
}
|
|
2463
|
+
const after = !!(where && typeof where === "object" && where.after);
|
|
2464
|
+
const name = typeof where === "string" ? where : String((after ? where.after : where.before) || "");
|
|
2465
|
+
const anchor = nodes.find((node) => node.dataset.kItem === name);
|
|
2466
|
+
if (!anchor) return mainEnd;
|
|
2467
|
+
return { parent: anchor.parentElement, ref: after ? anchor.nextElementSibling || null : anchor };
|
|
2468
|
+
}
|
|
2469
|
+
insertButton(button, where, { withSep } = {}) {
|
|
2470
|
+
const { parent, ref } = this.slotOf(where);
|
|
2471
|
+
if (!parent || !button) return;
|
|
2472
|
+
if (withSep && !ref) parent.appendChild(h("span.ke-tb-sep"));
|
|
2473
|
+
parent.insertBefore(button, ref || null);
|
|
2474
|
+
}
|
|
2475
|
+
/** 运行时把任意按钮(内置或自定义)移到指定位置 */
|
|
2476
|
+
moveButton(name, where) {
|
|
2477
|
+
var _a, _b;
|
|
2478
|
+
const node = this.buttonNodes().find((item) => item.dataset.kItem === String(name));
|
|
2479
|
+
if (!node) return false;
|
|
2480
|
+
node.remove();
|
|
2481
|
+
this.insertButton(node, where === void 0 ? ((_a = this.custom.get(String(name))) == null ? void 0 : _a.spec.position) ?? null : where);
|
|
2482
|
+
this.revealButton(node);
|
|
2483
|
+
(_b = this.syncScrollHints) == null ? void 0 : _b.call(this);
|
|
2484
|
+
return true;
|
|
2485
|
+
}
|
|
2486
|
+
/** 工具栏溢出时把按钮滚进可视区(避开 sticky 尾栏的遮挡) */
|
|
2487
|
+
revealButton(button) {
|
|
2488
|
+
const tb = this.el;
|
|
2489
|
+
if (!tb || !button || !button.isConnected) return;
|
|
2490
|
+
const max = tb.scrollWidth - tb.clientWidth;
|
|
2491
|
+
if (max <= 1) return;
|
|
2492
|
+
const tailW = this.tailEl ? this.tailEl.offsetWidth : 0;
|
|
2493
|
+
const left = button.offsetLeft;
|
|
2494
|
+
const right = left + button.offsetWidth;
|
|
2495
|
+
const viewR = tb.scrollLeft + tb.clientWidth - tailW;
|
|
2496
|
+
if (right > viewR - 4) tb.scrollLeft = Math.min(max, right - tb.clientWidth + tailW + 10);
|
|
2497
|
+
else if (left < tb.scrollLeft + 4) tb.scrollLeft = Math.max(0, left - 10);
|
|
2498
|
+
}
|
|
2499
|
+
/** 移除自定义按钮并中断其未完成的请求 */
|
|
2500
|
+
removeButton(name) {
|
|
2501
|
+
var _a, _b, _c, _d;
|
|
2502
|
+
const entry = this.custom.get(name);
|
|
2503
|
+
if (!entry) return false;
|
|
2504
|
+
abortRuntime(entry.rt);
|
|
2505
|
+
if (this.editor.popover.anchor === entry.button) this.editor.popover.close();
|
|
2506
|
+
const prev = (_a = entry.button) == null ? void 0 : _a.previousElementSibling;
|
|
2507
|
+
const next = (_b = entry.button) == null ? void 0 : _b.nextElementSibling;
|
|
2508
|
+
(_c = entry.button) == null ? void 0 : _c.remove();
|
|
2509
|
+
if (prev && prev.classList.contains("ke-tb-sep") && (!next || next.classList.contains("ke-tb-sep"))) prev.remove();
|
|
2510
|
+
this.buttons.delete(name);
|
|
2511
|
+
this.custom.delete(name);
|
|
2512
|
+
(_d = this.syncScrollHints) == null ? void 0 : _d.call(this);
|
|
2513
|
+
return true;
|
|
2514
|
+
}
|
|
2515
|
+
/** 外部灌入下拉数据(接口在编辑器之外请求时用),并刷新已打开的面板 */
|
|
2516
|
+
setButtonOptions(name, items) {
|
|
2517
|
+
const entry = this.custom.get(name);
|
|
2518
|
+
if (!entry) return null;
|
|
2519
|
+
const list = setRuntimeItems(entry.rt, items);
|
|
2520
|
+
if (this.editor.popover.isOpen && this.editor.popover.anchor === entry.button) {
|
|
2521
|
+
this.editor.popover.close();
|
|
2522
|
+
this.openCustom(name);
|
|
2523
|
+
}
|
|
2524
|
+
return list;
|
|
2525
|
+
}
|
|
2526
|
+
/** 读取按钮当前选中项 */
|
|
2527
|
+
getButtonValue(name) {
|
|
2528
|
+
const entry = this.custom.get(name);
|
|
2529
|
+
return entry ? entry.rt.value : null;
|
|
2530
|
+
}
|
|
2531
|
+
setButtonValue(name, value) {
|
|
2532
|
+
const entry = this.custom.get(name);
|
|
2533
|
+
if (!entry) return null;
|
|
2534
|
+
const items = entry.rt.items || [];
|
|
2535
|
+
const match = items.find((item) => String(item.value) === String(value)) || null;
|
|
2536
|
+
entry.rt.value = match;
|
|
2537
|
+
this.syncCustomLabel(name);
|
|
2538
|
+
return match;
|
|
2539
|
+
}
|
|
1689
2540
|
toggleColor(name, button) {
|
|
1690
2541
|
const kind = name === "foreColor" ? "fore" : "back";
|
|
1691
2542
|
this.togglePop(
|
|
@@ -1774,6 +2625,13 @@ class Toolbar {
|
|
|
1774
2625
|
if (button.dataset.kPop || !spec.pop) button.classList.toggle("ke-is-active", active);
|
|
1775
2626
|
if (spec.enabled) button.disabled = !spec.enabled(state);
|
|
1776
2627
|
}
|
|
2628
|
+
for (const [name, entry] of this.custom) {
|
|
2629
|
+
const { spec, button } = entry;
|
|
2630
|
+
if (!button) continue;
|
|
2631
|
+
if (spec.active) button.classList.toggle("ke-is-active", !!spec.active(state, this.editor));
|
|
2632
|
+
if (spec.enabled) button.disabled = !spec.enabled(state, this.editor);
|
|
2633
|
+
if (spec.type === "select") this.syncCustomLabel(name);
|
|
2634
|
+
}
|
|
1777
2635
|
if (this.headingEl) {
|
|
1778
2636
|
const match = HEADINGS.find((x) => x.value === state.block);
|
|
1779
2637
|
const label = this.headingEl.querySelector(".ke-btn-text");
|
|
@@ -1923,6 +2781,8 @@ class Toolbar {
|
|
|
1923
2781
|
}
|
|
1924
2782
|
if (this.onTbScroll) tb.removeEventListener("scroll", this.onTbScroll);
|
|
1925
2783
|
if (this.onTbPointerDown) tb.removeEventListener("pointerdown", this.onTbPointerDown);
|
|
2784
|
+
for (const entry of this.custom.values()) abortRuntime(entry.rt);
|
|
2785
|
+
this.custom.clear();
|
|
1926
2786
|
(_a = this.scrollFades) == null ? void 0 : _a.remove();
|
|
1927
2787
|
(_b = this.hintEl) == null ? void 0 : _b.remove();
|
|
1928
2788
|
this.scrollFades = null;
|
|
@@ -2038,210 +2898,6 @@ function closestBlock(node, root) {
|
|
|
2038
2898
|
}
|
|
2039
2899
|
return null;
|
|
2040
2900
|
}
|
|
2041
|
-
const ALLOWED_TAGS = /* @__PURE__ */ new Set([
|
|
2042
|
-
"a",
|
|
2043
|
-
"abbr",
|
|
2044
|
-
"b",
|
|
2045
|
-
"blockquote",
|
|
2046
|
-
"br",
|
|
2047
|
-
"code",
|
|
2048
|
-
"col",
|
|
2049
|
-
"colgroup",
|
|
2050
|
-
"dd",
|
|
2051
|
-
"del",
|
|
2052
|
-
"div",
|
|
2053
|
-
"dl",
|
|
2054
|
-
"dt",
|
|
2055
|
-
"em",
|
|
2056
|
-
"figcaption",
|
|
2057
|
-
"figure",
|
|
2058
|
-
"font",
|
|
2059
|
-
"h1",
|
|
2060
|
-
"h2",
|
|
2061
|
-
"h3",
|
|
2062
|
-
"h4",
|
|
2063
|
-
"h5",
|
|
2064
|
-
"h6",
|
|
2065
|
-
"hr",
|
|
2066
|
-
"i",
|
|
2067
|
-
"img",
|
|
2068
|
-
"ins",
|
|
2069
|
-
"kbd",
|
|
2070
|
-
"li",
|
|
2071
|
-
"mark",
|
|
2072
|
-
"ol",
|
|
2073
|
-
"p",
|
|
2074
|
-
"pre",
|
|
2075
|
-
"s",
|
|
2076
|
-
"samp",
|
|
2077
|
-
"section",
|
|
2078
|
-
"small",
|
|
2079
|
-
"span",
|
|
2080
|
-
"strong",
|
|
2081
|
-
"sub",
|
|
2082
|
-
"sup",
|
|
2083
|
-
"table",
|
|
2084
|
-
"tbody",
|
|
2085
|
-
"td",
|
|
2086
|
-
"tfoot",
|
|
2087
|
-
"th",
|
|
2088
|
-
"thead",
|
|
2089
|
-
"tr",
|
|
2090
|
-
"u",
|
|
2091
|
-
"ul",
|
|
2092
|
-
"video",
|
|
2093
|
-
"source"
|
|
2094
|
-
]);
|
|
2095
|
-
const GLOBAL_ATTRS = /* @__PURE__ */ new Set(["style", "class"]);
|
|
2096
|
-
const TAG_ATTRS = {
|
|
2097
|
-
A: ["href", "title", "target", "rel"],
|
|
2098
|
-
IMG: ["src", "alt", "title", "width", "height"],
|
|
2099
|
-
VIDEO: ["src", "poster", "controls", "loop", "muted", "playsinline", "preload", "width", "height"],
|
|
2100
|
-
SOURCE: ["src", "type"],
|
|
2101
|
-
FONT: ["size", "color"],
|
|
2102
|
-
TD: ["colspan", "rowspan"],
|
|
2103
|
-
TH: ["colspan", "rowspan"],
|
|
2104
|
-
OL: ["start"],
|
|
2105
|
-
LI: ["value"]
|
|
2106
|
-
};
|
|
2107
|
-
const DATA_PREFIX = ["data-k-", "data-keditor-"];
|
|
2108
|
-
const SAFE_URL_RE = /^(?:https?:|mailto:|tel:|blob:|\/|#|\.{1,2}\/|[^:]*$)/i;
|
|
2109
|
-
const SAFE_DATA_RE = /^data:image\/(?:png|jpe?g|gif|webp|svg\+xml);/i;
|
|
2110
|
-
function isSafeUrl(url) {
|
|
2111
|
-
if (!url) return false;
|
|
2112
|
-
const value = String(url).trim().replace(/\s+/g, "");
|
|
2113
|
-
if (/^(javascript|vbscript|file|about):/i.test(value)) return false;
|
|
2114
|
-
if (value.startsWith("data:")) return SAFE_DATA_RE.test(value);
|
|
2115
|
-
return SAFE_URL_RE.test(value);
|
|
2116
|
-
}
|
|
2117
|
-
function sanitizeStyle(value) {
|
|
2118
|
-
const out = [];
|
|
2119
|
-
for (const decl of String(value).split(";")) {
|
|
2120
|
-
const idx = decl.indexOf(":");
|
|
2121
|
-
if (idx < 0) continue;
|
|
2122
|
-
const prop = decl.slice(0, idx).trim().toLowerCase();
|
|
2123
|
-
const val = decl.slice(idx + 1).trim();
|
|
2124
|
-
if (!prop || !val) continue;
|
|
2125
|
-
if (!/^[a-z-]+$/.test(prop)) continue;
|
|
2126
|
-
if (prop.startsWith("mso-") || prop.startsWith("-mso-")) continue;
|
|
2127
|
-
if (/expression|javascript:|behavior:|url\(\s*['"]?\s*(javascript|data):/i.test(val)) continue;
|
|
2128
|
-
out.push(`${prop}:${val}`);
|
|
2129
|
-
}
|
|
2130
|
-
return out.join(";");
|
|
2131
|
-
}
|
|
2132
|
-
function sanitizeClass(value) {
|
|
2133
|
-
return String(value).split(/\s+/).filter((name) => /^[\w-]+$/.test(name) && !/^(script|javascript)/i.test(name)).join(" ");
|
|
2134
|
-
}
|
|
2135
|
-
function keepAttribute(tagName, attrName, attrValue) {
|
|
2136
|
-
const name = attrName.toLowerCase();
|
|
2137
|
-
if (name.startsWith("on")) return false;
|
|
2138
|
-
if (GLOBAL_ATTRS.has(name)) return true;
|
|
2139
|
-
if (DATA_PREFIX.some((prefix) => name.startsWith(prefix))) return true;
|
|
2140
|
-
const list = TAG_ATTRS[tagName] || [];
|
|
2141
|
-
return list.includes(name);
|
|
2142
|
-
}
|
|
2143
|
-
function sanitizeHtml(html, { allowEmpty = false } = {}) {
|
|
2144
|
-
if (!html) return allowEmpty ? "" : "<p><br></p>";
|
|
2145
|
-
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
2146
|
-
const root = doc.body.firstChild;
|
|
2147
|
-
walk(root);
|
|
2148
|
-
const result = root.innerHTML.trim();
|
|
2149
|
-
if (!result && !allowEmpty) return "<p><br></p>";
|
|
2150
|
-
return result;
|
|
2151
|
-
}
|
|
2152
|
-
function walk(node) {
|
|
2153
|
-
const children = Array.from(node.childNodes);
|
|
2154
|
-
for (const child of children) {
|
|
2155
|
-
if (child.nodeType === 8) {
|
|
2156
|
-
child.remove();
|
|
2157
|
-
continue;
|
|
2158
|
-
}
|
|
2159
|
-
if (child.nodeType === 3) continue;
|
|
2160
|
-
if (child.nodeType !== 1) {
|
|
2161
|
-
child.remove();
|
|
2162
|
-
continue;
|
|
2163
|
-
}
|
|
2164
|
-
const tagName = child.tagName;
|
|
2165
|
-
if (!ALLOWED_TAGS.has(tagName.toLowerCase())) {
|
|
2166
|
-
if (tagName === "SCRIPT" || tagName === "STYLE" || tagName === "IFRAME" || tagName === "OBJECT" || tagName === "EMBED" || tagName === "FORM" || tagName === "LINK" || tagName === "META") {
|
|
2167
|
-
child.remove();
|
|
2168
|
-
continue;
|
|
2169
|
-
}
|
|
2170
|
-
const parent = child.parentNode;
|
|
2171
|
-
while (child.firstChild) parent.insertBefore(child.firstChild, child);
|
|
2172
|
-
parent.removeChild(child);
|
|
2173
|
-
continue;
|
|
2174
|
-
}
|
|
2175
|
-
for (const attr of Array.from(child.attributes)) {
|
|
2176
|
-
if (!keepAttribute(tagName, attr.name, attr.value)) {
|
|
2177
|
-
child.removeAttribute(attr.name);
|
|
2178
|
-
continue;
|
|
2179
|
-
}
|
|
2180
|
-
const lower = attr.name.toLowerCase();
|
|
2181
|
-
if (lower === "style") child.setAttribute("style", sanitizeStyle(attr.value));
|
|
2182
|
-
else if (lower === "class") {
|
|
2183
|
-
const cls = sanitizeClass(attr.value);
|
|
2184
|
-
if (cls) child.setAttribute("class", cls);
|
|
2185
|
-
else child.removeAttribute("class");
|
|
2186
|
-
} else if (lower === "href" || lower === "src" || lower === "poster") {
|
|
2187
|
-
if (!isSafeUrl(attr.value)) child.removeAttribute(attr.name);
|
|
2188
|
-
}
|
|
2189
|
-
}
|
|
2190
|
-
if (tagName === "A" && child.getAttribute("target") === "_blank") child.setAttribute("rel", "noopener noreferrer");
|
|
2191
|
-
if (child.getAttribute("href") === "" || !child.getAttribute("href")) {
|
|
2192
|
-
if (tagName === "A") child.removeAttribute("href");
|
|
2193
|
-
}
|
|
2194
|
-
walk(child);
|
|
2195
|
-
}
|
|
2196
|
-
}
|
|
2197
|
-
const WORD_FONT_SIZES = { 1: 10, 2: 13, 3: 16, 4: 18, 5: 24, 6: 32, 7: 48 };
|
|
2198
|
-
function isWordHtml(html) {
|
|
2199
|
-
return /urn:schemas-microsoft-com|class=["']?Mso|ProgID|w:val=/i.test(html);
|
|
2200
|
-
}
|
|
2201
|
-
function cleanWordHtml(html) {
|
|
2202
|
-
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
2203
|
-
const root = doc.body.firstChild;
|
|
2204
|
-
root.querySelectorAll("*").forEach((node) => {
|
|
2205
|
-
const tag = node.tagName.toLowerCase();
|
|
2206
|
-
if (tag.startsWith("o:") || tag.startsWith("w:") || tag === "style" || tag === "script" || tag === "link" || tag === "meta") {
|
|
2207
|
-
node.remove();
|
|
2208
|
-
return;
|
|
2209
|
-
}
|
|
2210
|
-
if (tag === "img") {
|
|
2211
|
-
const width = Number(node.getAttribute("width") || 0);
|
|
2212
|
-
const height = Number(node.getAttribute("height") || 0);
|
|
2213
|
-
if (width && width <= 3 || height && height <= 3) node.remove();
|
|
2214
|
-
return;
|
|
2215
|
-
}
|
|
2216
|
-
if (tag === "font") {
|
|
2217
|
-
const size = WORD_FONT_SIZES[Number(node.getAttribute("size"))];
|
|
2218
|
-
const span = doc.createElement("span");
|
|
2219
|
-
const styles = [];
|
|
2220
|
-
if (size) styles.push(`font-size:${size}px`);
|
|
2221
|
-
if (node.color) styles.push(`color:${node.color}`);
|
|
2222
|
-
if (styles.length) span.setAttribute("style", styles.join(";"));
|
|
2223
|
-
while (node.firstChild) span.appendChild(node.firstChild);
|
|
2224
|
-
node.replaceWith(span);
|
|
2225
|
-
return;
|
|
2226
|
-
}
|
|
2227
|
-
for (const attr of Array.from(node.attributes)) {
|
|
2228
|
-
const name = attr.name.toLowerCase();
|
|
2229
|
-
if (name.startsWith("mso-") || name === "class" || name.startsWith("_") || name.startsWith("w:") || name.startsWith("o:") || name === "lang" || name === "dir") {
|
|
2230
|
-
node.removeAttribute(attr.name);
|
|
2231
|
-
}
|
|
2232
|
-
}
|
|
2233
|
-
});
|
|
2234
|
-
return root.innerHTML;
|
|
2235
|
-
}
|
|
2236
|
-
function escapeHtml(text) {
|
|
2237
|
-
return String(text).replace(/[&<>"']/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[ch]);
|
|
2238
|
-
}
|
|
2239
|
-
function toPlainText(html) {
|
|
2240
|
-
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, "text/html");
|
|
2241
|
-
doc.body.querySelectorAll("style,script").forEach((n) => n.remove());
|
|
2242
|
-
doc.body.innerHTML = doc.body.innerHTML.replace(/<\/(p|div|h[1-6]|li|blockquote|pre|tr)>/gi, "\n");
|
|
2243
|
-
return doc.body.textContent.replace(/\u00a0/g, " ").replace(/\n{3,}/g, "\n\n").trim();
|
|
2244
|
-
}
|
|
2245
2901
|
class Commands {
|
|
2246
2902
|
constructor(editor) {
|
|
2247
2903
|
this.editor = editor;
|
|
@@ -3395,7 +4051,16 @@ const defaults = {
|
|
|
3395
4051
|
minHeight: 320,
|
|
3396
4052
|
primaryColor: "#07c160",
|
|
3397
4053
|
paperWidth: 677,
|
|
4054
|
+
// 版心基准宽度:窄屏上限、宽屏最小宽度
|
|
4055
|
+
/** 版心宽度模式:'fluid' 宽屏随容器生长(两侧留白恒定)| 'fixed' 恒定版心 */
|
|
4056
|
+
paperMode: "fluid",
|
|
4057
|
+
paperGap: 24,
|
|
4058
|
+
// fluid 模式下单侧留白(px)
|
|
4059
|
+
paperMaxWidth: 0,
|
|
4060
|
+
// fluid 模式下版心最大宽度,0 = 不限
|
|
3398
4061
|
toolbar: DEFAULT_TOOLBAR,
|
|
4062
|
+
/** 自定义工具栏按钮(下拉选择,数据可静态配置或从接口读取) */
|
|
4063
|
+
customButtons: [],
|
|
3399
4064
|
hideToolbar: false,
|
|
3400
4065
|
hideStatusbar: false,
|
|
3401
4066
|
readOnly: false,
|
|
@@ -3430,6 +4095,7 @@ class KEditor {
|
|
|
3430
4095
|
this.options = mergeOptions(options);
|
|
3431
4096
|
this.id = `ke-${instanceSeq += 1}`;
|
|
3432
4097
|
this.el = resolveTarget(target, this.id);
|
|
4098
|
+
this.el.__keditor__ = this;
|
|
3433
4099
|
const hostHTML = this.el.innerHTML;
|
|
3434
4100
|
this.emitters = /* @__PURE__ */ new Map();
|
|
3435
4101
|
this.domListeners = [];
|
|
@@ -3445,7 +4111,7 @@ class KEditor {
|
|
|
3445
4111
|
this.uploader = createUploader(this.options.uploader, this.options.upload);
|
|
3446
4112
|
this.media = createMedia(this);
|
|
3447
4113
|
this.history = new History(this.root, { limit: this.options.historyLimit });
|
|
3448
|
-
this.toolbar = new Toolbar(this, this.options.toolbar);
|
|
4114
|
+
this.toolbar = new Toolbar(this, this.options.toolbar, this.options.customButtons);
|
|
3449
4115
|
this.source = new SourcePanel(this);
|
|
3450
4116
|
this.previewPanel = new PreviewOverlay(this);
|
|
3451
4117
|
this.bubble = new MediaBubble(this);
|
|
@@ -3495,8 +4161,30 @@ class KEditor {
|
|
|
3495
4161
|
this.scrollEl.style.height = `${this.options.height}px`;
|
|
3496
4162
|
this.scrollEl.style.maxHeight = `${this.options.height}px`;
|
|
3497
4163
|
}
|
|
3498
|
-
this.paperEl.style.setProperty("--ke-paper", `${this.options.paperWidth}px`);
|
|
3499
4164
|
this.paperEl.style.setProperty("--ke-min-height", `${this.options.minHeight}px`);
|
|
4165
|
+
this.applyPaper();
|
|
4166
|
+
}
|
|
4167
|
+
/** 版心尺寸:宽屏下默认让版心随容器生长,两侧只保留固定留白 */
|
|
4168
|
+
applyPaper() {
|
|
4169
|
+
const { paperWidth, paperMode, paperGap, paperMaxWidth } = this.options;
|
|
4170
|
+
this.paperEl.style.setProperty("--ke-paper", `${paperWidth}px`);
|
|
4171
|
+
this.paperEl.style.setProperty("--ke-paper-gap", `${paperGap}px`);
|
|
4172
|
+
if (paperMaxWidth > 0) this.paperEl.style.setProperty("--ke-paper-max", `${paperMaxWidth}px`);
|
|
4173
|
+
else this.paperEl.style.removeProperty("--ke-paper-max");
|
|
4174
|
+
this.paperEl.classList.toggle("ke-is-fluid", paperMode !== "fixed");
|
|
4175
|
+
}
|
|
4176
|
+
/** 运行时调整版心:{width, mode, gap, maxWidth}(mode: 'fluid' | 'fixed') */
|
|
4177
|
+
setPaper(config = {}) {
|
|
4178
|
+
if (config.width !== void 0) this.options.paperWidth = clamp$1(Number(config.width) || 0, 200, 8e3);
|
|
4179
|
+
if (config.mode !== void 0) this.options.paperMode = config.mode === "fixed" ? "fixed" : "fluid";
|
|
4180
|
+
if (config.gap !== void 0) this.options.paperGap = clamp$1(Number(config.gap) || 0, 0, 600);
|
|
4181
|
+
if (config.maxWidth !== void 0) this.options.paperMaxWidth = Math.max(0, Number(config.maxWidth) || 0);
|
|
4182
|
+
this.applyPaper();
|
|
4183
|
+
return { ...this.paperOptions() };
|
|
4184
|
+
}
|
|
4185
|
+
paperOptions() {
|
|
4186
|
+
const { paperWidth, paperMode, paperGap, paperMaxWidth } = this.options;
|
|
4187
|
+
return { width: paperWidth, mode: paperMode, gap: paperGap, maxWidth: paperMaxWidth };
|
|
3500
4188
|
}
|
|
3501
4189
|
applyTheme() {
|
|
3502
4190
|
if (this.options.primaryColor) this.el.style.setProperty("--ke-primary", this.options.primaryColor);
|
|
@@ -3753,8 +4441,8 @@ class KEditor {
|
|
|
3753
4441
|
const sel = window.getSelection();
|
|
3754
4442
|
if (!sel || sel.rangeCount === 0 || !this.root.contains(sel.anchorNode)) {
|
|
3755
4443
|
const range = document.createRange();
|
|
3756
|
-
const
|
|
3757
|
-
range.selectNodeContents(
|
|
4444
|
+
const first2 = this.root.firstElementChild || this.root;
|
|
4445
|
+
range.selectNodeContents(first2);
|
|
3758
4446
|
range.collapse(false);
|
|
3759
4447
|
sel.removeAllRanges();
|
|
3760
4448
|
sel.addRange(range);
|
|
@@ -3827,6 +4515,40 @@ class KEditor {
|
|
|
3827
4515
|
this.media.cancelAll();
|
|
3828
4516
|
return this;
|
|
3829
4517
|
}
|
|
4518
|
+
/* ------------------------------ 自定义工具栏按钮 ------------------------------ */
|
|
4519
|
+
/** 运行时新增自定义按钮(下拉选择 / 普通按钮),返回按钮 DOM */
|
|
4520
|
+
addButton(spec, options) {
|
|
4521
|
+
return this.toolbar.addButton(spec, options);
|
|
4522
|
+
}
|
|
4523
|
+
/** 运行时移动任意按钮位置(内置或自定义);where 同 spec.position 语义 */
|
|
4524
|
+
moveButton(name, where) {
|
|
4525
|
+
return this.toolbar.moveButton(name, where);
|
|
4526
|
+
}
|
|
4527
|
+
/** 移除自定义按钮,并中断其未完成的接口请求 */
|
|
4528
|
+
removeButton(name) {
|
|
4529
|
+
return this.toolbar.removeButton(name);
|
|
4530
|
+
}
|
|
4531
|
+
/** 外部灌入下拉数据(接口在编辑器之外请求时使用),返回规范化后的列表 */
|
|
4532
|
+
setButtonOptions(name, items) {
|
|
4533
|
+
return this.toolbar.setButtonOptions(name, items);
|
|
4534
|
+
}
|
|
4535
|
+
/** 读取 / 设置下拉按钮当前选中项 */
|
|
4536
|
+
getButtonValue(name) {
|
|
4537
|
+
return this.toolbar.getButtonValue(name);
|
|
4538
|
+
}
|
|
4539
|
+
setButtonValue(name, value) {
|
|
4540
|
+
return this.toolbar.setButtonValue(name, value);
|
|
4541
|
+
}
|
|
4542
|
+
/** 统一错误上报:toast 提示 + emit('error') + options.onError */
|
|
4543
|
+
reportError(type, error) {
|
|
4544
|
+
var _a, _b, _c;
|
|
4545
|
+
const message = error && error.message ? error.message : String(error);
|
|
4546
|
+
const payload = { type, error: error instanceof Error ? error : new Error(message), message };
|
|
4547
|
+
(_a = this.toast) == null ? void 0 : _a.error(message);
|
|
4548
|
+
this.emit("error", payload);
|
|
4549
|
+
(_c = (_b = this.options).onError) == null ? void 0 : _c.call(_b, payload);
|
|
4550
|
+
return this;
|
|
4551
|
+
}
|
|
3830
4552
|
/* ------------------------------ 面板 ------------------------------ */
|
|
3831
4553
|
toggleSource() {
|
|
3832
4554
|
if (this.sourceMode) this.exitSource();
|
|
@@ -3932,6 +4654,7 @@ class KEditor {
|
|
|
3932
4654
|
this.scrollEl.removeEventListener("scroll", this.scrollHandler);
|
|
3933
4655
|
window.removeEventListener("resize", this.scrollHandler);
|
|
3934
4656
|
if (this.fullscreen) document.body.style.overflow = "";
|
|
4657
|
+
if (this.el.__keditor__ === this) this.el.__keditor__ = null;
|
|
3935
4658
|
this.el.innerHTML = "";
|
|
3936
4659
|
this.el.classList.remove("ke", "ke-is-fullscreen", "ke-is-readonly");
|
|
3937
4660
|
this.el.removeAttribute("style");
|
|
@@ -3984,12 +4707,58 @@ function mergeOptions(options) {
|
|
|
3984
4707
|
out.image = { ...defaults.image, ...options.image || {} };
|
|
3985
4708
|
out.paste = { ...defaults.paste, ...options.paste || {} };
|
|
3986
4709
|
out.upload = { ...defaults.upload, ...options.upload || {} };
|
|
4710
|
+
out.customButtons = normalizeCustomButtons(options.customButtons);
|
|
3987
4711
|
if (Array.isArray(options.toolbar)) out.toolbar = options.toolbar;
|
|
3988
4712
|
if (options.toolbar === false) out.toolbar = [];
|
|
3989
4713
|
if (options.toolbar === true) out.toolbar = DEFAULT_TOOLBAR;
|
|
4714
|
+
if (out.customButtons.length && options.toolbar !== false) {
|
|
4715
|
+
out.toolbar = placeCustomNames(out.toolbar, out.customButtons);
|
|
4716
|
+
}
|
|
3990
4717
|
out.height = clamp$1(out.height ?? 460, 0, 4e3);
|
|
4718
|
+
out.paperWidth = clamp$1(Number(out.paperWidth) || defaults.paperWidth, 200, 8e3);
|
|
4719
|
+
out.paperMode = out.paperMode === "fixed" ? "fixed" : "fluid";
|
|
4720
|
+
out.paperGap = Number.isFinite(Number(out.paperGap)) ? clamp$1(Number(out.paperGap), 0, 600) : defaults.paperGap;
|
|
4721
|
+
out.paperMaxWidth = Math.max(0, Number(out.paperMaxWidth) || 0);
|
|
3991
4722
|
return out;
|
|
3992
4723
|
}
|
|
4724
|
+
const isButtonItem = (key) => key !== "|" && key !== ":tail";
|
|
4725
|
+
function placeCustomNames(items, specs) {
|
|
4726
|
+
const out = [...items];
|
|
4727
|
+
const loose = [];
|
|
4728
|
+
for (const spec of specs) {
|
|
4729
|
+
if (out.includes(spec.name)) continue;
|
|
4730
|
+
if (spec.position == null) {
|
|
4731
|
+
loose.push(spec.name);
|
|
4732
|
+
continue;
|
|
4733
|
+
}
|
|
4734
|
+
out.splice(arrIndexOf(out, spec.position), 0, spec.name);
|
|
4735
|
+
}
|
|
4736
|
+
if (loose.length) out.splice(arrIndexOf(out, null), 0, "|", ...loose);
|
|
4737
|
+
return out;
|
|
4738
|
+
}
|
|
4739
|
+
function arrIndexOf(items, where) {
|
|
4740
|
+
const mainEnd = (() => {
|
|
4741
|
+
const at2 = items.indexOf(":tail");
|
|
4742
|
+
return at2 < 0 ? items.length : at2;
|
|
4743
|
+
})();
|
|
4744
|
+
if (where == null || where === "end") return mainEnd;
|
|
4745
|
+
if (where === "start") return 0;
|
|
4746
|
+
if (where === "tail") return items.length;
|
|
4747
|
+
if (typeof where === "number") {
|
|
4748
|
+
let seen = 0;
|
|
4749
|
+
for (let i = 0; i < items.length; i++) {
|
|
4750
|
+
if (!isButtonItem(items[i])) continue;
|
|
4751
|
+
if (seen === Math.max(0, Math.floor(where))) return i;
|
|
4752
|
+
seen++;
|
|
4753
|
+
}
|
|
4754
|
+
return mainEnd;
|
|
4755
|
+
}
|
|
4756
|
+
const after = !!(where && typeof where === "object" && where.after);
|
|
4757
|
+
const name = String(typeof where === "string" ? where : (after ? where.after : where.before) || "");
|
|
4758
|
+
const at = items.indexOf(name);
|
|
4759
|
+
if (at < 0) return mainEnd;
|
|
4760
|
+
return after ? at + 1 : at;
|
|
4761
|
+
}
|
|
3993
4762
|
function buildStatusbar(editor) {
|
|
3994
4763
|
const chars = h("b", { text: "0" });
|
|
3995
4764
|
const words = h("b", { text: "0" });
|
|
@@ -4081,7 +4850,7 @@ function previewHtml(html) {
|
|
|
4081
4850
|
});
|
|
4082
4851
|
return root.innerHTML;
|
|
4083
4852
|
}
|
|
4084
|
-
const version = "1.0.
|
|
4853
|
+
const version = "1.0.3";
|
|
4085
4854
|
function create(target, options = {}) {
|
|
4086
4855
|
return new KEditor(target, options);
|
|
4087
4856
|
}
|
|
@@ -4109,6 +4878,9 @@ export {
|
|
|
4109
4878
|
maybeCompress,
|
|
4110
4879
|
mockAdapter,
|
|
4111
4880
|
mount,
|
|
4881
|
+
normalizeCustomButtons,
|
|
4882
|
+
normalizeItems,
|
|
4883
|
+
normalizePosition,
|
|
4112
4884
|
replace,
|
|
4113
4885
|
sanitizeHtml,
|
|
4114
4886
|
simpleAdapter,
|