kyle-editor 1.0.10 → 1.1.0

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.
@@ -2,6 +2,80 @@
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.KEditor = {}));
3
3
  })(this, function(exports2) {
4
4
  "use strict";
5
+ const DEFAULT_FONT_SIZE = 14;
6
+ const uid = (prefix = "k") => `${prefix}${Date.now().toString(36)}${Math.random().toString(36).slice(2, 7)}`;
7
+ function debounce(fn, wait = 300) {
8
+ let timer = null;
9
+ const wrapped = (...args) => {
10
+ clearTimeout(timer);
11
+ timer = setTimeout(() => fn(...args), wait);
12
+ };
13
+ wrapped.cancel = () => clearTimeout(timer);
14
+ wrapped.flush = (...args) => {
15
+ clearTimeout(timer);
16
+ fn(...args);
17
+ };
18
+ return wrapped;
19
+ }
20
+ const clamp$1 = (num, min, max) => Math.min(Math.max(num, min), max);
21
+ function formatSize(bytes) {
22
+ if (!bytes && bytes !== 0) return "-";
23
+ const units = ["B", "KB", "MB", "GB"];
24
+ let i = 0;
25
+ let value = bytes;
26
+ while (value >= 1024 && i < units.length - 1) {
27
+ value /= 1024;
28
+ i += 1;
29
+ }
30
+ return `${value.toFixed(value >= 10 || i === 0 ? 0 : 1)}${units[i]}`;
31
+ }
32
+ function h(spec, attrs = {}, children = []) {
33
+ let tag = "div";
34
+ let className = "";
35
+ const parts = String(spec).split(".");
36
+ tag = parts[0] || "div";
37
+ className = parts.slice(1).join(" ");
38
+ const node = document.createElement(tag);
39
+ if (className) node.className = className;
40
+ for (const [key, val] of Object.entries(attrs || {})) {
41
+ if (val === void 0 || val === null || val === false) continue;
42
+ if (key === "class") node.className = `${node.className} ${val}`.trim();
43
+ else if (key === "style" && typeof val === "object") Object.assign(node.style, val);
44
+ else if (key === "html") node.innerHTML = val;
45
+ else if (key === "text") node.textContent = val;
46
+ else if (key === "dataset") Object.assign(node.dataset, val);
47
+ else if (key.startsWith("on") && typeof val === "function") node.addEventListener(key.slice(2).toLowerCase(), val);
48
+ else node.setAttribute(key, val === true ? "" : String(val));
49
+ }
50
+ const list = Array.isArray(children) ? children : [children];
51
+ for (const child of list) {
52
+ if (child === null || child === void 0 || child === false) continue;
53
+ node.appendChild(typeof child === "string" || typeof child === "number" ? document.createTextNode(String(child)) : child);
54
+ }
55
+ return node;
56
+ }
57
+ function getMediaAlign(img) {
58
+ if (!img) return "left";
59
+ const ml = img.style.marginLeft;
60
+ const mr = img.style.marginRight;
61
+ if (ml === "auto" && mr === "auto") return "center";
62
+ if (ml === "auto") return "right";
63
+ return "left";
64
+ }
65
+ function setMediaAlign(img, align) {
66
+ if (!img) return;
67
+ img.style.maxWidth = "100%";
68
+ if (align === "center") {
69
+ img.style.marginLeft = "auto";
70
+ img.style.marginRight = "auto";
71
+ } else if (align === "right") {
72
+ img.style.marginLeft = "auto";
73
+ img.style.marginRight = "0";
74
+ } else {
75
+ img.style.marginLeft = "0";
76
+ img.style.marginRight = "auto";
77
+ }
78
+ }
5
79
  const CSS$1 = `
6
80
  .ke, .ke * { box-sizing: border-box; }
7
81
  .ke {
@@ -197,7 +271,7 @@
197
271
  .ke-hd-sample { display: block; color: #1f2329; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1.35; }
198
272
  .ke-hd-meta { display: flex; align-items: baseline; gap: 6px; font-size: 11.5px; color: #a2a8b3; }
199
273
  .ke-hd-label { color: #6b7280; }
200
- .ke-hd-row .s-body { font-size: 15px; }
274
+ .ke-hd-row .s-body { font-size: ${DEFAULT_FONT_SIZE}px; }
201
275
  .ke-hd-row .s-h1 { font-size: 21px; font-weight: 700; }
202
276
  .ke-hd-row .s-h2 { font-size: 18px; font-weight: 700; }
203
277
  .ke-hd-row .s-h3 { font-size: 16px; font-weight: 600; }
@@ -215,7 +289,14 @@
215
289
  .ke-panel-icon .ke-ico { width: 13px; height: 13px; }
216
290
  .ke-panel-icon + .ke-panel-note { margin-left: 8px; }
217
291
 
292
+ /* 下拉选择面板:宽度固定,内容过长时在面板内横向滚动(否则会被根节点 overflow:hidden 裁掉) */
293
+ .ke-panel-select { width: 300px; box-sizing: border-box; }
294
+ .ke-panel-select .ke-panel-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
295
+ .ke-panel-select .ke-panel-note { flex: none; max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
218
296
  .ke-panel-select .ke-sel-search { margin-bottom: 8px; }
297
+ .ke-panel-select .ke-sel-body { overflow: auto; }
298
+ .ke-panel-select .ke-sel-row { width: max-content; min-width: 100%; box-sizing: border-box; }
299
+ .ke-panel-select .ke-sel-label, .ke-panel-select .ke-sel-desc { overflow: visible; text-overflow: clip; }
219
300
  .ke-code-panel .ke-sel-search { margin-bottom: 8px; }
220
301
  .ke-code-list { display: flex; flex-direction: column; max-height: 252px; overflow-y: auto; overscroll-behavior: contain; margin: 0 -4px; padding: 0 4px; }
221
302
  .ke-code-panel .ke-panel-hint { margin-top: 10px; }
@@ -345,7 +426,7 @@
345
426
  width: min(100%, max(calc(var(--ke-paper) + 64px), calc(100% - var(--ke-paper-gap) * 2)));
346
427
  max-width: var(--ke-paper-max, none);
347
428
  }
348
- .ke-content { outline: none; font-size: 17px; line-height: 1.75; color: var(--ke-text); word-break: break-word; }
429
+ .ke-content { outline: none; font-size: ${DEFAULT_FONT_SIZE}px; line-height: 1.75; color: var(--ke-text); word-break: break-word; }
349
430
  .ke-content:empty::before,
350
431
  .ke-content[data-placeholder].ke-is-empty::before {
351
432
  content: attr(data-placeholder); color: #b8bec7; pointer-events: none;
@@ -365,7 +446,7 @@
365
446
  }
366
447
  .ke-content a { color: #576b95; text-decoration: none; border-bottom: 1px solid rgba(87,107,149,.35); }
367
448
  .ke-content hr { border: 0; border-top: 1px solid var(--ke-line); margin: 24px 0; }
368
- .ke-content code { background: #f2f3f5; border-radius: 4px; padding: 2px 6px; font-size: 14px; font-family: 'SFMono-Regular', Consolas, Menlo, monospace; color: #c7254e; }
449
+ .ke-content code { background: #f2f3f5; border-radius: 4px; padding: 2px 6px; font-size: ${DEFAULT_FONT_SIZE - 1}px; font-family: 'SFMono-Regular', Consolas, Menlo, monospace; color: #c7254e; }
369
450
  .ke-content pre {
370
451
  position: relative;
371
452
  background: #282c34; color: #e6e6e6; padding: 14px 16px; border-radius: 8px; overflow-x: auto;
@@ -530,7 +611,7 @@
530
611
  .ke-preview-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: #fff; border-bottom: 1px solid var(--ke-line); font-size: 12px; color: var(--ke-muted); }
531
612
  .ke-preview-body { flex: 1; overflow: auto; padding: 20px; }
532
613
  .ke-preview-frame { margin: 0 auto; background: #fff; border-radius: 10px; box-shadow: var(--ke-shadow); padding: 22px 20px; transition: width .2s ease; }
533
- .ke-preview-frame .ke-content { font-size: 17px; line-height: 1.75; color: var(--ke-text); }
614
+ .ke-preview-frame .ke-content { font-size: ${DEFAULT_FONT_SIZE}px; line-height: 1.75; color: var(--ke-text); }
534
615
  .ke-preview-frame { min-height: 120px; }
535
616
 
536
617
  /* ---------------- ReadOnly / disabled ---------------- */
@@ -567,79 +648,6 @@
567
648
  document.head.appendChild(style);
568
649
  injected = true;
569
650
  }
570
- const uid = (prefix = "k") => `${prefix}${Date.now().toString(36)}${Math.random().toString(36).slice(2, 7)}`;
571
- function debounce(fn, wait = 300) {
572
- let timer = null;
573
- const wrapped = (...args) => {
574
- clearTimeout(timer);
575
- timer = setTimeout(() => fn(...args), wait);
576
- };
577
- wrapped.cancel = () => clearTimeout(timer);
578
- wrapped.flush = (...args) => {
579
- clearTimeout(timer);
580
- fn(...args);
581
- };
582
- return wrapped;
583
- }
584
- const clamp$1 = (num, min, max) => Math.min(Math.max(num, min), max);
585
- function formatSize(bytes) {
586
- if (!bytes && bytes !== 0) return "-";
587
- const units = ["B", "KB", "MB", "GB"];
588
- let i = 0;
589
- let value = bytes;
590
- while (value >= 1024 && i < units.length - 1) {
591
- value /= 1024;
592
- i += 1;
593
- }
594
- return `${value.toFixed(value >= 10 || i === 0 ? 0 : 1)}${units[i]}`;
595
- }
596
- function h(spec, attrs = {}, children = []) {
597
- let tag = "div";
598
- let className = "";
599
- const parts = String(spec).split(".");
600
- tag = parts[0] || "div";
601
- className = parts.slice(1).join(" ");
602
- const node = document.createElement(tag);
603
- if (className) node.className = className;
604
- for (const [key, val] of Object.entries(attrs || {})) {
605
- if (val === void 0 || val === null || val === false) continue;
606
- if (key === "class") node.className = `${node.className} ${val}`.trim();
607
- else if (key === "style" && typeof val === "object") Object.assign(node.style, val);
608
- else if (key === "html") node.innerHTML = val;
609
- else if (key === "text") node.textContent = val;
610
- else if (key === "dataset") Object.assign(node.dataset, val);
611
- else if (key.startsWith("on") && typeof val === "function") node.addEventListener(key.slice(2).toLowerCase(), val);
612
- else node.setAttribute(key, val === true ? "" : String(val));
613
- }
614
- const list = Array.isArray(children) ? children : [children];
615
- for (const child of list) {
616
- if (child === null || child === void 0 || child === false) continue;
617
- node.appendChild(typeof child === "string" || typeof child === "number" ? document.createTextNode(String(child)) : child);
618
- }
619
- return node;
620
- }
621
- function getMediaAlign(img) {
622
- if (!img) return "left";
623
- const ml = img.style.marginLeft;
624
- const mr = img.style.marginRight;
625
- if (ml === "auto" && mr === "auto") return "center";
626
- if (ml === "auto") return "right";
627
- return "left";
628
- }
629
- function setMediaAlign(img, align) {
630
- if (!img) return;
631
- img.style.maxWidth = "100%";
632
- if (align === "center") {
633
- img.style.marginLeft = "auto";
634
- img.style.marginRight = "auto";
635
- } else if (align === "right") {
636
- img.style.marginLeft = "auto";
637
- img.style.marginRight = "0";
638
- } else {
639
- img.style.marginLeft = "0";
640
- img.style.marginRight = "auto";
641
- }
642
- }
643
651
  const S = (d) => `<svg class="ke-ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="${d}"/></svg>`;
644
652
  const icons = {
645
653
  undo: `<svg class="ke-ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 14 4 9l5-5"/><path d="M4 9h10a5 5 0 0 1 0 10h-3"/></svg>`,
@@ -2527,6 +2535,14 @@ ${items}
2527
2535
  const box = anchor.getBoundingClientRect();
2528
2536
  const visTop = Math.max(0, -rootBox.top);
2529
2537
  const visBottom = Math.min(rootBox.height, window.innerHeight - rootBox.top);
2538
+ if (rootBox.width > 0) {
2539
+ const limit = Math.max(160, Math.min(rootBox.width, window.innerWidth) - 16);
2540
+ const inner = this.el.firstElementChild;
2541
+ if (inner) {
2542
+ inner.style.removeProperty("max-width");
2543
+ if (inner.getBoundingClientRect().width > limit) inner.style.maxWidth = `${limit}px`;
2544
+ }
2545
+ }
2530
2546
  const popBox = this.el.getBoundingClientRect();
2531
2547
  let left = box.left - rootBox.left;
2532
2548
  if (align === "center") left = box.left - rootBox.left + box.width / 2 - popBox.width / 2;
@@ -2904,18 +2920,19 @@ ${items}
2904
2920
  return wrap;
2905
2921
  }
2906
2922
  function fontSizePanel(sizes, { value, onPick }) {
2907
- const wrap = panel("字号", h("span.ke-panel-note", { text: value ? `${value}px` : "默认 17px" }));
2923
+ const current = Number(value) || DEFAULT_FONT_SIZE;
2924
+ const wrap = panel("字号", h("span.ke-panel-note", { text: `${current}px` }));
2908
2925
  wrap.classList.add("ke-panel-wide");
2909
2926
  const grid = h("div.ke-size-grid");
2910
2927
  sizes.forEach((size) => {
2911
2928
  const btn2 = h("button.ke-size-cell", { type: "button", text: String(size), onclick: () => onPick(size) });
2912
2929
  btn2.style.fontSize = `${Math.min(20, 10 + size / 6)}px`;
2913
- if (Number(value) === size) btn2.classList.add("ke-is-active");
2930
+ if (current === size) btn2.classList.add("ke-is-active");
2914
2931
  grid.appendChild(btn2);
2915
2932
  });
2916
2933
  wrap.appendChild(grid);
2917
2934
  wrap.appendChild(divider());
2918
- wrap.appendChild(h("div.ke-panel-hint", { text: "仅作用于选中的文字;未选中时对新输入的文字生效" }));
2935
+ wrap.appendChild(h("div.ke-panel-hint", { text: `当前 ${current}px;仅作用于选中的文字,未选中时对新输入的文字生效` }));
2919
2936
  return wrap;
2920
2937
  }
2921
2938
  ({ text: THEME_COLORS.map((x) => x[0]), bg: BG_COLORS.map((x) => x[0]) });
@@ -3128,8 +3145,10 @@ ${items}
3128
3145
  function selectPanel({ title, note, searchable, width, remote, value, load, reload, linkOf, onPick, emptyText }) {
3129
3146
  const wrap = panel(title);
3130
3147
  wrap.classList.add("ke-panel-wide", "ke-panel-select");
3131
- if (width) wrap.style.width = `${Math.min(420, Math.max(200, Number(width)))}px`;
3148
+ if (Number(width) > 0) wrap.style.width = `${Math.min(420, Math.max(200, Number(width)))}px`;
3132
3149
  const head = wrap.querySelector(".ke-panel-head");
3150
+ const titleEl = head.querySelector(".ke-panel-title");
3151
+ if (titleEl) titleEl.title = title;
3133
3152
  const countEl = h("span.ke-panel-note", { text: note || "" });
3134
3153
  if (remote) {
3135
3154
  head.appendChild(h("button.ke-panel-icon", {
@@ -3984,7 +4003,7 @@ ${items}
3984
4003
  const SCROLL_HINT_KEY = "keditor.toolbarScrollHint";
3985
4004
  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>';
3986
4005
  const HEADINGS = [
3987
- { value: "p", label: "正文", desc: "默认段落 · 17px,正文内容都用它", sample: "正文示例文字", sampleClass: "s-body" },
4006
+ { value: "p", label: "正文", desc: `默认段落 · ${DEFAULT_FONT_SIZE}px,正文内容都用它`, sample: "正文示例文字", sampleClass: "s-body" },
3988
4007
  { value: "h1", label: "标题一", desc: "大标题 · 22px 加粗,全文主标题(建议只用一次)", sample: "标题一示例", sampleClass: "s-h1" },
3989
4008
  { value: "h2", label: "标题二", desc: "章节标题 · 20px 加粗,划分小节", sample: "标题二示例", sampleClass: "s-h2" },
3990
4009
  { value: "h3", label: "标题三", desc: "小标题 · 18px 加粗,章节内的层级", sample: "标题三示例", sampleClass: "s-h3" }
@@ -4201,7 +4220,7 @@ ${items}
4201
4220
  onclick: (event) => {
4202
4221
  event.preventDefault();
4203
4222
  this.togglePop("fontsize", button, () => fontSizePanel(FONT_SIZES, {
4204
- value: this.editor.getState().fontSize,
4223
+ value: this.editor.getState().fontSize || DEFAULT_FONT_SIZE,
4205
4224
  onPick: (size) => {
4206
4225
  this.editor.restoreSelection();
4207
4226
  this.editor.commands.setFontSize(Number(size));
@@ -4573,7 +4592,8 @@ ${items}
4573
4592
  }
4574
4593
  if (this.fontSizeEl) {
4575
4594
  const label = this.fontSizeEl.querySelector(".ke-btn-text");
4576
- if (label) label.textContent = state.fontSize ? `${state.fontSize}px` : "字号";
4595
+ if (label) label.textContent = `${state.fontSize || DEFAULT_FONT_SIZE}px`;
4596
+ this.fontSizeEl.title = `字号(当前 ${state.fontSize || DEFAULT_FONT_SIZE}px,默认 ${DEFAULT_FONT_SIZE}px)`;
4577
4597
  }
4578
4598
  if (state.linkUrl) {
4579
4599
  const btn2 = this.buttons.get("link");
@@ -7133,7 +7153,7 @@ ${items}
7133
7153
  });
7134
7154
  return root.innerHTML;
7135
7155
  }
7136
- const version = "1.0.10";
7156
+ const version = "1.1.0";
7137
7157
  function create(target, options = {}) {
7138
7158
  return new KEditor(target, options);
7139
7159
  }