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