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