@usememos/editor 26.10.3 → 26.10.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/base.css +20 -0
- package/dist/index.js +90 -8
- package/dist/index.js.map +2 -2
- package/dist/style.css +34 -0
- package/dist/theme.css +3 -0
- package/dist/types/editor.d.ts +4 -0
- package/dist/types/icons.d.ts +11 -0
- package/dist/types/labels.d.ts +4 -0
- package/dist/types/markdown/serialize.d.ts +4 -4
- package/dist/types/view/dom.d.ts +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
|
|
|
59
59
|
| `attributes` | `Record<string, string>` | Attributes for the editable root, such as `autocorrect`, `autocapitalize` or `aria-label`. |
|
|
60
60
|
| `resolveUrl` | `(url, kind) => string \| null` | The URL to use for a link (`kind` `'link'`) or an image (`'image'`), or `null` to drop it: the link is not clickable, the image has no `src`. The saved Markdown keeps the original. Default `defaultResolveUrl`, exported: relative URLs, `http`, `https`, `mailto` and `tel`. Call it from your own policy to extend it, for example to allow `sms:` or to block `http:` images. |
|
|
61
61
|
| `headingIds` | `(headings) => (string \| undefined)[]` | Element ids for headings, for in-page links and an outline. Called with every heading's `{ text, level }` in order, again when the document changes. |
|
|
62
|
+
| `codeCopy` | `boolean` | A copy button at the end of each code block's bar, shown on hover or focus, that copies the code. Default `true`. Its text is `labels.copyCode` and `labels.copied`, its icons `icons.copyCode` and `icons.copied`. |
|
|
62
63
|
| `referrerPolicy` | `ReferrerPolicy` | `referrerpolicy` for images, such as `no-referrer`. Images always load lazily. |
|
|
63
64
|
| `onLinkClick` | `({ editor, href, event }) => boolean` | Called when a link is clicked to be opened: a plain click while read-only, Mod+click while editing. Return `true` to handle it, for example to route an in-app link or scroll to an anchor; otherwise the editor opens `href` in a new tab. |
|
|
64
65
|
| `theme` | `{ classes?, tokens?, colorScheme? }` | Your own classes on rendered elements, colors and fonts, and light or dark. See [Theming](#theming). |
|
package/dist/base.css
CHANGED
|
@@ -103,6 +103,26 @@
|
|
|
103
103
|
|
|
104
104
|
/* Code */
|
|
105
105
|
.mbe-root .mbe-code-bar { display: flex; align-items: center; }
|
|
106
|
+
/* The copy button sits at the end of the bar and shows on hover or focus; always on touch screens. */
|
|
107
|
+
.mbe-root .mbe-code-copy {
|
|
108
|
+
margin-inline-start: auto;
|
|
109
|
+
display: inline-flex;
|
|
110
|
+
align-items: center;
|
|
111
|
+
justify-content: center;
|
|
112
|
+
border: 0;
|
|
113
|
+
background: none;
|
|
114
|
+
padding: 2px;
|
|
115
|
+
font: inherit;
|
|
116
|
+
color: inherit;
|
|
117
|
+
cursor: pointer;
|
|
118
|
+
opacity: 0;
|
|
119
|
+
}
|
|
120
|
+
.mbe-root .mbe-code:hover .mbe-code-copy,
|
|
121
|
+
.mbe-root .mbe-code-copy:focus-visible,
|
|
122
|
+
.mbe-root .mbe-code-copy.mbe-copied { opacity: 1; }
|
|
123
|
+
@media (hover: none) {
|
|
124
|
+
.mbe-root .mbe-code-copy { opacity: 1; }
|
|
125
|
+
}
|
|
106
126
|
.mbe-root .mbe-code-lang {
|
|
107
127
|
font: inherit;
|
|
108
128
|
color: inherit;
|
package/dist/index.js
CHANGED
|
@@ -1998,6 +1998,10 @@ var DEFAULT_LABELS = {
|
|
|
1998
1998
|
language: "Language",
|
|
1999
1999
|
/** Shown in a code block's language field when it has none. */
|
|
2000
2000
|
plainText: "text",
|
|
2001
|
+
/** The copy button of a code block. */
|
|
2002
|
+
copyCode: "Copy code",
|
|
2003
|
+
/** The copy button of a code block, just after copying. */
|
|
2004
|
+
copied: "Copied",
|
|
2001
2005
|
addRowAbove: "Add row above",
|
|
2002
2006
|
addRowBelow: "Add row below",
|
|
2003
2007
|
addColumnLeft: "Add column left",
|
|
@@ -2037,6 +2041,10 @@ var ICONS = {
|
|
|
2037
2041
|
divider: ["M5 12h14"],
|
|
2038
2042
|
/** `copy` */
|
|
2039
2043
|
duplicate: [["rect", { width: "14", height: "14", x: "8", y: "8", rx: "2", ry: "2" }], "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"],
|
|
2044
|
+
/** `copy` */
|
|
2045
|
+
copyCode: [["rect", { width: "14", height: "14", x: "8", y: "8", rx: "2", ry: "2" }], "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"],
|
|
2046
|
+
/** `check` */
|
|
2047
|
+
copied: ["M20 6 9 17l-5-5"],
|
|
2040
2048
|
/** `trash` */
|
|
2041
2049
|
delete: TRASH,
|
|
2042
2050
|
/** `arrow-up` */
|
|
@@ -3997,21 +4005,27 @@ function representableInline(inline3) {
|
|
|
3997
4005
|
endRun();
|
|
3998
4006
|
for (const [from, to] of removals) spans = setMarkInRange(spans, from, to, mark, false);
|
|
3999
4007
|
}
|
|
4000
|
-
return
|
|
4008
|
+
return dropLineEdgeSpaces(spans);
|
|
4001
4009
|
}
|
|
4002
4010
|
var isPlainSpan = (s) => !s.link && !s.image && !s.token && !s.marks.includes("code") && !s.marks.includes("html");
|
|
4003
|
-
function
|
|
4011
|
+
function dropLineEdgeSpaces(spans) {
|
|
4004
4012
|
const text = spans.map((s) => s.text).join("");
|
|
4005
|
-
if (!/[ \t](\n|$)/.test(text)) return spans;
|
|
4013
|
+
if (!/[ \t](\n|$)|(^|\n)[ \t]/.test(text)) return spans;
|
|
4006
4014
|
const plain2 = [];
|
|
4007
4015
|
for (const s of spans) for (let i = 0; i < s.text.length; i++) plain2.push(isPlainSpan(s));
|
|
4008
4016
|
const removals = [];
|
|
4009
4017
|
let lineStart = 0;
|
|
4010
4018
|
for (let i = 0; i <= text.length; i++) {
|
|
4011
4019
|
if (i < text.length && text[i] !== "\n") continue;
|
|
4020
|
+
const space = (k) => (text[k] === " " || text[k] === " ") && plain2[k];
|
|
4021
|
+
let to = lineStart;
|
|
4022
|
+
while (to < i && space(to)) to++;
|
|
4012
4023
|
let from = i;
|
|
4013
|
-
while (from > lineStart && (
|
|
4014
|
-
if (
|
|
4024
|
+
while (from > lineStart && space(from - 1)) from--;
|
|
4025
|
+
if (to < from) {
|
|
4026
|
+
if (to > lineStart) removals.push([lineStart, to]);
|
|
4027
|
+
if (from < i) removals.push([from, i]);
|
|
4028
|
+
}
|
|
4015
4029
|
lineStart = i + 1;
|
|
4016
4030
|
}
|
|
4017
4031
|
let out = spans;
|
|
@@ -5749,6 +5763,7 @@ function renderBlock(block3, ctx, reveal) {
|
|
|
5749
5763
|
lang.setAttribute("aria-label", labels.language);
|
|
5750
5764
|
lang.placeholder = labels.plainText;
|
|
5751
5765
|
bar.appendChild(lang);
|
|
5766
|
+
if (ctx.codeCopy) bar.appendChild(copyButton(ctx));
|
|
5752
5767
|
const pre = el("pre");
|
|
5753
5768
|
const code = el("code");
|
|
5754
5769
|
code.spellcheck = false;
|
|
@@ -5828,6 +5843,17 @@ function fillContent(target, inline3, ctx, block3, reveal, cell, decorated = tru
|
|
|
5828
5843
|
const last = inline3[inline3.length - 1];
|
|
5829
5844
|
if (last && last.text.endsWith("\n")) target.appendChild(fill(doc));
|
|
5830
5845
|
}
|
|
5846
|
+
function copyButton(ctx) {
|
|
5847
|
+
const button = ctx.doc.createElement("button");
|
|
5848
|
+
button.type = "button";
|
|
5849
|
+
button.className = "mbe-code-copy";
|
|
5850
|
+
button.tabIndex = -1;
|
|
5851
|
+
button.setAttribute("aria-label", ctx.labels.copyCode);
|
|
5852
|
+
button.title = ctx.labels.copyCode;
|
|
5853
|
+
const icon = ctx.icon?.("copyCode");
|
|
5854
|
+
if (icon) button.appendChild(icon);
|
|
5855
|
+
return button;
|
|
5856
|
+
}
|
|
5831
5857
|
function fill(doc) {
|
|
5832
5858
|
const br = doc.createElement("br");
|
|
5833
5859
|
br.setAttribute(FILL_ATTR, "");
|
|
@@ -5998,6 +6024,10 @@ var BlockRenderer = class {
|
|
|
5998
6024
|
views;
|
|
5999
6025
|
/** Theme classes for rendered elements. Set before a full render. */
|
|
6000
6026
|
classes = NO_CLASSES;
|
|
6027
|
+
/** The icon of a UI control. */
|
|
6028
|
+
icon;
|
|
6029
|
+
/** Code blocks get a copy button. */
|
|
6030
|
+
codeCopy = false;
|
|
6001
6031
|
/** Per block, the views that show their source. A block re-renders when its entry changes. */
|
|
6002
6032
|
revealed = /* @__PURE__ */ new Map();
|
|
6003
6033
|
/**
|
|
@@ -6021,7 +6051,9 @@ var BlockRenderer = class {
|
|
|
6021
6051
|
decorateCode: this.decorateCode,
|
|
6022
6052
|
views: this.views,
|
|
6023
6053
|
view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
|
|
6024
|
-
classes: this.classes
|
|
6054
|
+
classes: this.classes,
|
|
6055
|
+
icon: this.icon,
|
|
6056
|
+
codeCopy: this.codeCopy
|
|
6025
6057
|
};
|
|
6026
6058
|
}
|
|
6027
6059
|
renderView(syntax, source, kind) {
|
|
@@ -6662,6 +6694,8 @@ var Editor = class {
|
|
|
6662
6694
|
this.runtime = { ...this.runtime, resolveUrl: options.resolveUrl, onLinkClick: options.onLinkClick };
|
|
6663
6695
|
this.renderer.resolveUrl = options.resolveUrl ?? defaultResolveUrl;
|
|
6664
6696
|
this.renderer.referrerPolicy = options.referrerPolicy;
|
|
6697
|
+
this.renderer.codeCopy = options.codeCopy !== false;
|
|
6698
|
+
this.renderer.icon = (name) => iconNode(root.ownerDocument, this.icon(name));
|
|
6665
6699
|
this.applyTheme(options.theme);
|
|
6666
6700
|
setPopupContainer(root, options.popupContainer);
|
|
6667
6701
|
const configs = this.extensions.flatMap((e) => e.suggestion ? [e.suggestion] : []);
|
|
@@ -6939,8 +6973,13 @@ var Editor = class {
|
|
|
6939
6973
|
applyRuntime(options) {
|
|
6940
6974
|
const root = this.root;
|
|
6941
6975
|
if ("placeholder" in options) {
|
|
6942
|
-
if (options.placeholder)
|
|
6943
|
-
|
|
6976
|
+
if (options.placeholder) {
|
|
6977
|
+
root.style.setProperty("--mbe-placeholder", JSON.stringify(options.placeholder));
|
|
6978
|
+
root.setAttribute("aria-placeholder", options.placeholder);
|
|
6979
|
+
} else {
|
|
6980
|
+
root.style.removeProperty("--mbe-placeholder");
|
|
6981
|
+
root.removeAttribute("aria-placeholder");
|
|
6982
|
+
}
|
|
6944
6983
|
this.runtime = { ...this.runtime, placeholder: options.placeholder };
|
|
6945
6984
|
}
|
|
6946
6985
|
if ("attributes" in options) {
|
|
@@ -7958,6 +7997,10 @@ var Editor = class {
|
|
|
7958
7997
|
onMouseDown(e) {
|
|
7959
7998
|
this.caretKey = null;
|
|
7960
7999
|
const target = e.target;
|
|
8000
|
+
if (target.closest?.(".mbe-code-copy")) {
|
|
8001
|
+
e.preventDefault();
|
|
8002
|
+
return;
|
|
8003
|
+
}
|
|
7961
8004
|
this.dropBlockSelection();
|
|
7962
8005
|
if (target === this.root && e.button === 0 && !this.readOnly && this.clickBelowLastBlock(e)) return;
|
|
7963
8006
|
const view = e.button === 0 && !this.readOnly ? target.closest?.(".mbe-view, .mbe-view-block") : null;
|
|
@@ -8000,6 +8043,12 @@ var Editor = class {
|
|
|
8000
8043
|
if (e.detail === 0) this.toggleTaskAt(target);
|
|
8001
8044
|
return;
|
|
8002
8045
|
}
|
|
8046
|
+
const copy = target.closest?.(".mbe-code-copy");
|
|
8047
|
+
if (copy) {
|
|
8048
|
+
e.preventDefault();
|
|
8049
|
+
this.copyCode(copy);
|
|
8050
|
+
return;
|
|
8051
|
+
}
|
|
8003
8052
|
const link = target.closest?.("a.mbe-link");
|
|
8004
8053
|
if (!link) return;
|
|
8005
8054
|
e.preventDefault();
|
|
@@ -8009,6 +8058,39 @@ var Editor = class {
|
|
|
8009
8058
|
this.root.ownerDocument.defaultView?.open(href, "_blank", "noopener,noreferrer");
|
|
8010
8059
|
}
|
|
8011
8060
|
}
|
|
8061
|
+
/** Copies the code of the code block holding `button`, and shows that it did for a moment. */
|
|
8062
|
+
copyCode(button) {
|
|
8063
|
+
const blockEl = button.closest(`[${BLOCK_ATTR}]`);
|
|
8064
|
+
const block3 = blockEl && this.state.doc.blocks.find((b) => b.id === blockEl.getAttribute(BLOCK_ATTR));
|
|
8065
|
+
if (!block3 || block3.type !== "code") return;
|
|
8066
|
+
const text = inlineText(block3.content);
|
|
8067
|
+
const doc = this.root.ownerDocument;
|
|
8068
|
+
const show = (name) => {
|
|
8069
|
+
const icon = iconNode(doc, this.icon(name));
|
|
8070
|
+
button.replaceChildren(...icon ? [icon] : []);
|
|
8071
|
+
button.setAttribute("aria-label", this.labels[name]);
|
|
8072
|
+
button.title = this.labels[name];
|
|
8073
|
+
button.classList.toggle("mbe-copied", name === "copied");
|
|
8074
|
+
};
|
|
8075
|
+
const done = () => {
|
|
8076
|
+
show("copied");
|
|
8077
|
+
setTimeout(() => button.isConnected && show("copyCode"), 1500);
|
|
8078
|
+
};
|
|
8079
|
+
const fallback = () => {
|
|
8080
|
+
const area = doc.createElement("textarea");
|
|
8081
|
+
area.value = text;
|
|
8082
|
+
area.style.cssText = "position:fixed;top:0;left:0;opacity:0";
|
|
8083
|
+
doc.body.appendChild(area);
|
|
8084
|
+
area.select();
|
|
8085
|
+
const ok = doc.execCommand("copy");
|
|
8086
|
+
area.remove();
|
|
8087
|
+
this.syncDomSelection(this.hasFocus());
|
|
8088
|
+
if (ok) done();
|
|
8089
|
+
};
|
|
8090
|
+
const clipboard = doc.defaultView?.navigator.clipboard;
|
|
8091
|
+
if (clipboard?.writeText) clipboard.writeText(text).then(done, fallback);
|
|
8092
|
+
else fallback();
|
|
8093
|
+
}
|
|
8012
8094
|
/** The code block language input. */
|
|
8013
8095
|
onControlChange(e) {
|
|
8014
8096
|
const input = e.target;
|