@usememos/editor 26.10.20 → 26.10.21

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
@@ -251,6 +251,8 @@ math({ render: (tex, element, options) => katex.render(tex, element, { ...option
251
251
 
252
252
  `blockView` draws whole blocks the app wants to show its own way, such as a paragraph that is one bare link shown as a preview card with data from the app's server. `match(block)` picks the blocks, and `render(block, element, { editor })` draws one into `element`, a div the editor owns and keeps while the block is unchanged: typing elsewhere, a new selection, `redecorate()` and a switch to read-only never call `render` again, so app UI mounted into the element, such as a React portal, stays. `render` runs again for a block whose content changed and for a new block, and may return a function, which the editor calls when it lets the element go: the block was removed or changed, `refreshViews()` ran, or the editor was destroyed. Returning `false` leaves the block as the editor draws it. While editing, the block shows as the editor draws it, editable, when the selection is in it, and a click on the view puts the caret there, as for a formula; read-only always shows the view. `refreshViews(name)` with the extension's name draws its blocks again.
253
253
 
254
+ `placement: 'after'` keeps the block as the editor draws it, editable, and places the element after it instead, like a widget: for UI that adds to the text rather than standing in for it, such as a card for a link that sits on its own line of a paragraph. `inlineLines(content)` gives a block's lines, the spans between its line breaks, so `match` can look at one line. The element is still withheld while the selection is in the block, so a link being typed is not previewed on every keystroke, and clicks on it are the app's, not the editor's.
255
+
254
256
  ```tsx
255
257
  const linkCards: Extension = {
256
258
  name: 'linkCards',
@@ -263,6 +265,22 @@ const linkCards: Extension = {
263
265
  },
264
266
  },
265
267
  };
268
+
269
+ // A card below a paragraph for each line of it that is one bare link (with `markdown: { breaks: true }`).
270
+ const bareLink = (line: Inline) => (line.length === 1 && line[0].link === line[0].text ? line[0].link : undefined);
271
+ const linkLines = (block: Block) => (block.type === 'paragraph' ? inlineLines(block.content).map(bareLink).filter((href) => href !== undefined) : []);
272
+ const linkCardsBelow: Extension = {
273
+ name: 'linkCardsBelow',
274
+ blockView: {
275
+ placement: 'after',
276
+ match: (block) => linkLines(block).length > 0,
277
+ render: (block, element) => {
278
+ const root = createRoot(element);
279
+ root.render(linkLines(block).map((href) => <LinkCard key={href} href={href} />));
280
+ return () => root.unmount();
281
+ },
282
+ },
283
+ };
266
284
  ```
267
285
 
268
286
  ### Theming
package/dist/base.css CHANGED
@@ -221,6 +221,8 @@
221
221
  cursor: pointer;
222
222
  opacity: 0;
223
223
  }
224
+ /* The icon matches the language label's 12px text, so the button is as tall as the label's line. */
225
+ .mbe-root .mbe-code-copy .mbe-icon { width: 12px; height: 12px; }
224
226
  .mbe-root .mbe-code:hover .mbe-code-copy,
225
227
  .mbe-root .mbe-code-copy:focus-visible,
226
228
  .mbe-root .mbe-code-copy.mbe-copied { opacity: 1; }
@@ -251,6 +253,8 @@
251
253
  :is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
252
254
  /* A block an extension's block view draws (see Extension.blockView): the app owns what is inside, so nothing is clipped. */
253
255
  .mbe-root > .mbe-block-view { overflow: visible; }
256
+ /* A block view placed after its block, which stays editable text: not text itself, like a widget. */
257
+ .mbe-root > .mbe-block-after { cursor: default; white-space: normal; }
254
258
  /* Tags and mentions with an onClick, read-only: clickable, as links are. */
255
259
  .mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
256
260
  /* Rendered HTML: line breaks between its tags are not text. The Markdown
package/dist/index.js CHANGED
@@ -233,6 +233,18 @@ function inlineLength(inline3) {
233
233
  for (const s of inline3) n += s.text.length;
234
234
  return n;
235
235
  }
236
+ function inlineLines(inline3) {
237
+ const lines = [[]];
238
+ for (const span of inline3) {
239
+ const parts = span.text.split("\n");
240
+ for (let i = 0; i < parts.length; i++) {
241
+ if (i > 0) lines.push([]);
242
+ const text = parts[i];
243
+ if (text) lines[lines.length - 1].push(text === span.text ? span : { ...span, text });
244
+ }
245
+ }
246
+ return lines;
247
+ }
236
248
  function normalizeInline(inline3) {
237
249
  const out = [];
238
250
  for (const span of inline3) {
@@ -1623,13 +1635,15 @@ function selectedBlocks(state) {
1623
1635
  const { from, to } = orderedRange(state.doc, state.selection);
1624
1636
  const start = blockIndex(state.doc, from.blockId);
1625
1637
  const end = blockIndex(state.doc, to.blockId);
1626
- return state.doc.blocks.slice(start, end + 1).map((block3) => {
1627
- if (!isTextBlock(block3)) {
1628
- if (block3.type === "table" && start === end && from.cell && sameCell(from.cell, to.cell)) {
1629
- return paragraph(sliceInline(contentOf(block3, from.cell), from.offset, to.offset));
1630
- }
1631
- return cloneBlock(block3);
1638
+ if (start === end) {
1639
+ const block3 = state.doc.blocks[start];
1640
+ if (isTextBlock(block3)) return [paragraph(sliceInline(block3.content, from.offset, to.offset))];
1641
+ if (block3.type === "table" && from.cell && sameCell(from.cell, to.cell)) {
1642
+ return [paragraph(sliceInline(contentOf(block3, from.cell), from.offset, to.offset))];
1632
1643
  }
1644
+ }
1645
+ return state.doc.blocks.slice(start, end + 1).map((block3) => {
1646
+ if (!isTextBlock(block3)) return cloneBlock(block3);
1633
1647
  const a = block3.id === from.blockId ? from.offset : 0;
1634
1648
  const b = block3.id === to.blockId ? to.offset : Infinity;
1635
1649
  return cloneBlock(withContent(block3, sliceInline(block3.content, a, b)));
@@ -7815,7 +7829,9 @@ var BlockRenderer = class {
7815
7829
  /**
7816
7830
  * Per block drawn by a block view, the element the view drew into (null
7817
7831
  * when every matching view declined), kept while the block object is the
7818
- * same, and the function to call when the element is let go.
7832
+ * same, and the function to call when the element is let go. A view placed
7833
+ * after its block has `after`: the editor's wrapper around that element,
7834
+ * placed after the block's element.
7819
7835
  */
7820
7836
  hosted = /* @__PURE__ */ new Map();
7821
7837
  /** The document's Markdown options, for the Markdown inside an HTML section. */
@@ -7909,13 +7925,15 @@ var BlockRenderer = class {
7909
7925
  return this.blockViews.some(({ view }) => view.match(block3));
7910
7926
  }
7911
7927
  /**
7912
- * The element a block view drew `block` into: the one from the last render
7913
- * while the block object is the same, else a fresh one from the first
7914
- * matching view that does not decline. Null when none draws it.
7928
+ * The element a block view drew `block` into, to show in place of the
7929
+ * block: the one from the last render while the block object is the same,
7930
+ * else a fresh one from the first matching view that does not decline.
7931
+ * Null when none draws it, and for a view placed after the block, whose
7932
+ * element `afterView` gives.
7915
7933
  */
7916
7934
  renderBlockView(block3) {
7917
7935
  const prev = this.hosted.get(block3.id);
7918
- if (prev && prev.block === block3) return prev.el;
7936
+ if (prev && prev.block === block3) return prev.after ? null : prev.el;
7919
7937
  if (prev) this.disposeHosted(block3.id);
7920
7938
  const ctx = this.viewContext;
7921
7939
  if (!ctx) return null;
@@ -7931,12 +7949,27 @@ var BlockRenderer = class {
7931
7949
  result = false;
7932
7950
  }
7933
7951
  if (result === false) continue;
7934
- this.hosted.set(block3.id, { block: block3, name, el, dispose: typeof result === "function" ? result : void 0 });
7935
- return el;
7952
+ const dispose = typeof result === "function" ? result : void 0;
7953
+ if (view.placement !== "after") {
7954
+ this.hosted.set(block3.id, { block: block3, name, el, dispose });
7955
+ return el;
7956
+ }
7957
+ const after = this.root.ownerDocument.createElement("div");
7958
+ after.className = "mbe-block-view mbe-block-after";
7959
+ after.contentEditable = "false";
7960
+ after.appendChild(el);
7961
+ this.hosted.set(block3.id, { block: block3, name, el, after, dispose });
7962
+ return null;
7936
7963
  }
7937
7964
  if (matched !== null) this.hosted.set(block3.id, { block: block3, name: matched, el: null });
7938
7965
  return null;
7939
7966
  }
7967
+ /** The element of a block view placed after `block`, while the block shows the view (not its editable form). */
7968
+ afterView(block3, reveal) {
7969
+ if (reveal?.has(REVEAL_BLOCK) || this.source) return void 0;
7970
+ const entry = this.hosted.get(block3.id);
7971
+ return entry?.block === block3 ? entry.after : void 0;
7972
+ }
7940
7973
  disposeHosted(id) {
7941
7974
  const entry = this.hosted.get(id);
7942
7975
  if (!entry) return;
@@ -8041,8 +8074,10 @@ var BlockRenderer = class {
8041
8074
  this.loadedImages.clear();
8042
8075
  for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
8043
8076
  const nodes = [];
8044
- for (const [id, { el }] of next) {
8077
+ for (const [id, { block: block3, el, reveal }] of next) {
8045
8078
  nodes.push(el);
8079
+ const after = this.afterView(block3, reveal);
8080
+ if (after) nodes.push(after);
8046
8081
  const widgets = this.widgetsAfter?.(id);
8047
8082
  if (widgets) nodes.push(...widgets);
8048
8083
  }
@@ -8927,6 +8962,7 @@ var ALIGN_CHOICES = [
8927
8962
  { id: "alignRight", align: "right", image: "imageAlignRight" }
8928
8963
  ];
8929
8964
  var SLASH_CHARS = ["/", "\uFF0F"];
8965
+ var APP_UI = ".mbe-widget, .mbe-block-after";
8930
8966
  var CARET_KEY = /^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/;
8931
8967
  var COMPOSITION_INPUT = /* @__PURE__ */ new Set(["insertCompositionText", "deleteCompositionText", "insertFromComposition", "deleteByComposition"]);
8932
8968
  var Editor = class {
@@ -9322,7 +9358,7 @@ var Editor = class {
9322
9358
  }
9323
9359
  hasFocus() {
9324
9360
  const active = this.root.ownerDocument.activeElement;
9325
- return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(".mbe-widget");
9361
+ return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(APP_UI);
9326
9362
  }
9327
9363
  // -------------------------------------------------------------------------
9328
9364
  // Widgets
@@ -10285,7 +10321,7 @@ var Editor = class {
10285
10321
  // Input
10286
10322
  /** Events from the code-language input and other form controls inside the root are not editor input. */
10287
10323
  fromControl(e) {
10288
- return e.target instanceof HTMLInputElement || !!e.target?.closest?.(".mbe-widget");
10324
+ return e.target instanceof HTMLInputElement || !!e.target?.closest?.(APP_UI);
10289
10325
  }
10290
10326
  onBeforeInput(e) {
10291
10327
  if (this.fromControl(e)) return;
@@ -10941,8 +10977,12 @@ var Editor = class {
10941
10977
  this.refreshSelection();
10942
10978
  if (isCollapsed(this.state.selection) || !e.clipboardData) return;
10943
10979
  e.preventDefault();
10944
- const doc = { blocks: closeSections(selectedBlocks(this.state)) };
10945
- writeClipboard(e.clipboardData, doc, serializeMarkdown(doc, this.markdown).replace(/\n$/, ""));
10980
+ const selected = selectedBlocks(this.state);
10981
+ const doc = { blocks: closeSections(selected) };
10982
+ const markdown = serializeMarkdown(doc, this.markdown).replace(/\n$/, "");
10983
+ const only = selected.length === 1 && selected[0].type === "paragraph" ? selected[0] : null;
10984
+ const code = only && isPlainTextBlock(getBlock(this.state.doc, this.state.selection.focus.blockId));
10985
+ writeClipboard(e.clipboardData, doc, markdown, code ? inlineText(only.content) : markdown);
10946
10986
  if (cut && !this.readOnly) this.run(deleteSelection);
10947
10987
  }
10948
10988
  onPaste(e) {
@@ -11702,9 +11742,9 @@ function blockName(block3) {
11702
11742
  return block3.type;
11703
11743
  }
11704
11744
  }
11705
- function writeClipboard(data, doc, text) {
11706
- data.setData("text/plain", text);
11707
- data.setData("text/html", `<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(text)}">${docToHtml(doc)}</div>`);
11745
+ function writeClipboard(data, doc, markdown, plain2 = markdown) {
11746
+ data.setData("text/plain", plain2);
11747
+ data.setData("text/html", `<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(markdown)}">${docToHtml(doc)}</div>`);
11708
11748
  }
11709
11749
  function sameBlocks(a, b) {
11710
11750
  return a.blocks.length === b.blocks.length && a.blocks.every((block3, i) => block3 === b.blocks[i]);
@@ -12161,6 +12201,7 @@ export {
12161
12201
  htmlToDoc,
12162
12202
  imageBlockOffset,
12163
12203
  inlineLength,
12204
+ inlineLines,
12164
12205
  inlineText,
12165
12206
  insertInline,
12166
12207
  isCollapsed,