@usememos/editor 26.10.3 → 26.10.4

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 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` */
@@ -5749,6 +5757,7 @@ function renderBlock(block3, ctx, reveal) {
5749
5757
  lang.setAttribute("aria-label", labels.language);
5750
5758
  lang.placeholder = labels.plainText;
5751
5759
  bar.appendChild(lang);
5760
+ if (ctx.codeCopy) bar.appendChild(copyButton(ctx));
5752
5761
  const pre = el("pre");
5753
5762
  const code = el("code");
5754
5763
  code.spellcheck = false;
@@ -5828,6 +5837,17 @@ function fillContent(target, inline3, ctx, block3, reveal, cell, decorated = tru
5828
5837
  const last = inline3[inline3.length - 1];
5829
5838
  if (last && last.text.endsWith("\n")) target.appendChild(fill(doc));
5830
5839
  }
5840
+ function copyButton(ctx) {
5841
+ const button = ctx.doc.createElement("button");
5842
+ button.type = "button";
5843
+ button.className = "mbe-code-copy";
5844
+ button.tabIndex = -1;
5845
+ button.setAttribute("aria-label", ctx.labels.copyCode);
5846
+ button.title = ctx.labels.copyCode;
5847
+ const icon = ctx.icon?.("copyCode");
5848
+ if (icon) button.appendChild(icon);
5849
+ return button;
5850
+ }
5831
5851
  function fill(doc) {
5832
5852
  const br = doc.createElement("br");
5833
5853
  br.setAttribute(FILL_ATTR, "");
@@ -5998,6 +6018,10 @@ var BlockRenderer = class {
5998
6018
  views;
5999
6019
  /** Theme classes for rendered elements. Set before a full render. */
6000
6020
  classes = NO_CLASSES;
6021
+ /** The icon of a UI control. */
6022
+ icon;
6023
+ /** Code blocks get a copy button. */
6024
+ codeCopy = false;
6001
6025
  /** Per block, the views that show their source. A block re-renders when its entry changes. */
6002
6026
  revealed = /* @__PURE__ */ new Map();
6003
6027
  /**
@@ -6021,7 +6045,9 @@ var BlockRenderer = class {
6021
6045
  decorateCode: this.decorateCode,
6022
6046
  views: this.views,
6023
6047
  view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
6024
- classes: this.classes
6048
+ classes: this.classes,
6049
+ icon: this.icon,
6050
+ codeCopy: this.codeCopy
6025
6051
  };
6026
6052
  }
6027
6053
  renderView(syntax, source, kind) {
@@ -6662,6 +6688,8 @@ var Editor = class {
6662
6688
  this.runtime = { ...this.runtime, resolveUrl: options.resolveUrl, onLinkClick: options.onLinkClick };
6663
6689
  this.renderer.resolveUrl = options.resolveUrl ?? defaultResolveUrl;
6664
6690
  this.renderer.referrerPolicy = options.referrerPolicy;
6691
+ this.renderer.codeCopy = options.codeCopy !== false;
6692
+ this.renderer.icon = (name) => iconNode(root.ownerDocument, this.icon(name));
6665
6693
  this.applyTheme(options.theme);
6666
6694
  setPopupContainer(root, options.popupContainer);
6667
6695
  const configs = this.extensions.flatMap((e) => e.suggestion ? [e.suggestion] : []);
@@ -7958,6 +7986,10 @@ var Editor = class {
7958
7986
  onMouseDown(e) {
7959
7987
  this.caretKey = null;
7960
7988
  const target = e.target;
7989
+ if (target.closest?.(".mbe-code-copy")) {
7990
+ e.preventDefault();
7991
+ return;
7992
+ }
7961
7993
  this.dropBlockSelection();
7962
7994
  if (target === this.root && e.button === 0 && !this.readOnly && this.clickBelowLastBlock(e)) return;
7963
7995
  const view = e.button === 0 && !this.readOnly ? target.closest?.(".mbe-view, .mbe-view-block") : null;
@@ -8000,6 +8032,12 @@ var Editor = class {
8000
8032
  if (e.detail === 0) this.toggleTaskAt(target);
8001
8033
  return;
8002
8034
  }
8035
+ const copy = target.closest?.(".mbe-code-copy");
8036
+ if (copy) {
8037
+ e.preventDefault();
8038
+ this.copyCode(copy);
8039
+ return;
8040
+ }
8003
8041
  const link = target.closest?.("a.mbe-link");
8004
8042
  if (!link) return;
8005
8043
  e.preventDefault();
@@ -8009,6 +8047,39 @@ var Editor = class {
8009
8047
  this.root.ownerDocument.defaultView?.open(href, "_blank", "noopener,noreferrer");
8010
8048
  }
8011
8049
  }
8050
+ /** Copies the code of the code block holding `button`, and shows that it did for a moment. */
8051
+ copyCode(button) {
8052
+ const blockEl = button.closest(`[${BLOCK_ATTR}]`);
8053
+ const block3 = blockEl && this.state.doc.blocks.find((b) => b.id === blockEl.getAttribute(BLOCK_ATTR));
8054
+ if (!block3 || block3.type !== "code") return;
8055
+ const text = inlineText(block3.content);
8056
+ const doc = this.root.ownerDocument;
8057
+ const show = (name) => {
8058
+ const icon = iconNode(doc, this.icon(name));
8059
+ button.replaceChildren(...icon ? [icon] : []);
8060
+ button.setAttribute("aria-label", this.labels[name]);
8061
+ button.title = this.labels[name];
8062
+ button.classList.toggle("mbe-copied", name === "copied");
8063
+ };
8064
+ const done = () => {
8065
+ show("copied");
8066
+ setTimeout(() => button.isConnected && show("copyCode"), 1500);
8067
+ };
8068
+ const fallback = () => {
8069
+ const area = doc.createElement("textarea");
8070
+ area.value = text;
8071
+ area.style.cssText = "position:fixed;top:0;left:0;opacity:0";
8072
+ doc.body.appendChild(area);
8073
+ area.select();
8074
+ const ok = doc.execCommand("copy");
8075
+ area.remove();
8076
+ this.syncDomSelection(this.hasFocus());
8077
+ if (ok) done();
8078
+ };
8079
+ const clipboard = doc.defaultView?.navigator.clipboard;
8080
+ if (clipboard?.writeText) clipboard.writeText(text).then(done, fallback);
8081
+ else fallback();
8082
+ }
8012
8083
  /** The code block language input. */
8013
8084
  onControlChange(e) {
8014
8085
  const input = e.target;