@usememos/editor 26.10.22 → 26.10.24

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
@@ -305,9 +305,45 @@ new Editor({
305
305
  });
306
306
  ```
307
307
 
308
- Colors are `text`, `muted`, `background`, `accent`, `link`, `codeBackground`, `border`, `popupBackground`, `popupBorder`, `hover`, `danger`, and `calloutNote`, `calloutTip`, `calloutImportant`, `calloutWarning` and `calloutCaution` (each callout type's icon, and a light tint of it behind the text); the other tokens are `font`, `mono`, `fontSize`, `lineHeight`, `radius` and `blockGap` (numbers are pixels). Values can be any CSS, such as `var(--primary)`, so the editor can follow your app's own variables. Menus, the toolbar and the link popover take the same tokens.
308
+ Colors are `text`, `muted`, `background`, `accent`, `link`, `codeBackground`, `border`, `popupBackground`, `popupBorder`, `hover`, `danger`, `highlight` (behind `<mark>` in rendered HTML), and `calloutNote`, `calloutTip`, `calloutImportant`, `calloutWarning` and `calloutCaution` (each callout type's icon, and a light tint of it behind the text); the other tokens are `font`, `mono`, `fontSize`, `lineHeight`, `radius` and `blockGap` (numbers are pixels). Values can be any CSS, such as `var(--primary)`, so the editor can follow your app's own variables; `colors` sets such a value for both schemes at once, and `light` and `dark` override it per scheme. Menus, the toolbar and the link popover take the same tokens.
309
309
 
310
- `colorScheme` picks light or dark: `auto` (the default) follows `prefers-color-scheme`, or `data-theme="light"` or `data-theme="dark"` on the editor or an ancestor; `light` and `dark` force one; `inherit` follows the page's `color-scheme`, for apps that switch it themselves. Switching tokens or the scheme with `setOptions({ theme })` re-renders nothing.
310
+ `fontSize` and `lineHeight` size the whole document, not only its paragraphs: headings are 1.5, 1.25 and 1.125 times `fontSize` (h4 to h6 the body size), code blocks, raw blocks and tables 0.875 times it, and the code block's language label 0.75. Each keeps the body's leading, the room `lineHeight` leaves around `fontSize`, so `fontSize: 14` with `lineHeight: 20` gives a 21px h1 on 27px lines, and code at 12.25px on 18.25px lines.
311
+
312
+ An app that keeps its palette in CSS variables passes them as the values, and the editor follows the app's theme, light and dark included, since the variables change with it. With the names most Tailwind-based component kits use:
313
+
314
+ ```ts
315
+ new Editor({
316
+ element,
317
+ theme: {
318
+ tokens: {
319
+ colors: {
320
+ text: 'var(--foreground)',
321
+ muted: 'var(--muted-foreground)',
322
+ accent: 'var(--primary)',
323
+ codeBackground: 'var(--muted)',
324
+ border: 'var(--border)',
325
+ popupBackground: 'var(--popover)',
326
+ hover: 'var(--accent)',
327
+ danger: 'var(--destructive)',
328
+ },
329
+ font: 'inherit',
330
+ radius: 'calc(var(--radius) - 2px)',
331
+ },
332
+ },
333
+ });
334
+ ```
335
+
336
+ The same tokens can be set in CSS, with no JavaScript, as `--mbe-<name>-light` and `--mbe-<name>-dark` (`--mbe-accent-light`, `--mbe-code-bg-dark`; the names are in `theme.css`). They inherit, so set them on `:root` for every editor on the page, or on a container for the editors in it. `theme.css` reads them with the default palette as fallback; a value you leave out keeps the default:
337
+
338
+ ```css
339
+ :root {
340
+ --mbe-accent-light: #0969da;
341
+ --mbe-accent-dark: #58a6ff;
342
+ --mbe-font: Inter, sans-serif;
343
+ }
344
+ ```
345
+
346
+ `colorScheme` picks light or dark: `auto` (the default) follows `prefers-color-scheme`, or `data-theme="light"` or `data-theme="dark"`, or a `light` or `dark` class (as Tailwind-style theming toggles it), on the editor or an ancestor; `light` and `dark` force one; `inherit` follows the page's `color-scheme`, for apps that switch it themselves. Switching tokens or the scheme with `setOptions({ theme })` re-renders nothing.
311
347
 
312
348
  The `theme` option's `classes` add your own classes to rendered elements, next to the editor's `mbe-*` classes, so the editor can share classes with the rest of your app:
313
349
 
@@ -408,6 +444,8 @@ With `mode: 'source'`, the editor shows the whole document as Markdown text, as
408
444
  <MarkdownEditor value={markdown} onChange={setMarkdown} mode={showSource ? 'source' : 'rich'} />
409
445
  ```
410
446
 
447
+ The text is set in the `mono` font at the body's size and line height, as code blocks are, so list markers and indentation line up.
448
+
411
449
  **Switching** is a change of view, not an edit. The text shown in source mode is exactly `getMarkdown()`. Going back to rich mode loads the text as `setMarkdown` does, so `getMarkdown()` returns it byte for byte, including lines edited in source mode. A switch emits no `change`, adds no undo step and does not move focus. Undo and redo work across it: undoing an edit made in the other mode restores it in the current mode. The caret stays in the same block, and keeps its column when the text before it in that block has no Markdown syntax; otherwise it goes to the start of the block's text. A switch requested while an input method is composing waits for the composition to end.
412
450
 
413
451
  **What works in source mode.** Source mode is plain text editing, like a `<textarea>` or a code block, that knows Markdown lists. It has no autocomplete and no formatting keys.
package/dist/base.css CHANGED
@@ -241,7 +241,7 @@
241
241
  /* Code keeps its lines and scrolls sideways, as GitHub shows it; raw source wraps. */
242
242
  :is(.mbe-root, .mbe-flow) > .mbe-code pre { white-space: pre; overflow-x: auto; tab-size: 2; }
243
243
  :is(.mbe-root, .mbe-flow) > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
244
- /* Source mode: the document as plain text in the editor's font. */
244
+ /* Source mode: the document as plain text; theme.css sets its mono font. */
245
245
  .mbe-root > .mbe-source { white-space: pre-wrap; tab-size: 4; overflow-wrap: anywhere; }
246
246
  :is(.mbe-root, .mbe-flow) > .mbe-code pre { margin: 0; }
247
247
  :is(.mbe-root, .mbe-flow) > .mbe-code pre code { display: block; }
package/dist/index.js CHANGED
@@ -7619,7 +7619,6 @@ function htmlRuns(inline3) {
7619
7619
  let depth = 0;
7620
7620
  for (let j = i + 1; j < spans.length; j++) {
7621
7621
  const t = spans[j].span;
7622
- if (t.link) break;
7623
7622
  if (!isHtml(t)) continue;
7624
7623
  const tag = /^<(\/?)([A-Za-z][A-Za-z0-9-]*)\b[^>]*?(\/?)>$/.exec(t.text);
7625
7624
  if (!tag || tag[2].toLowerCase() !== name || tag[3]) continue;
@@ -7631,7 +7630,6 @@ function htmlRuns(inline3) {
7631
7630
  }
7632
7631
  }
7633
7632
  }
7634
- if (spans.slice(i, end + 1).some((s) => s.span.link)) continue;
7635
7633
  runs.push({ from, to: end === i ? to : spans[end].to });
7636
7634
  i = end;
7637
7635
  }
@@ -7654,59 +7652,89 @@ function renderInline(inline3, parent, ctx, decorations, reveal, cell) {
7654
7652
  offsets.set(piece, offset);
7655
7653
  offset += piece.span.text.length;
7656
7654
  }
7657
- let i = 0;
7658
- while (i < pieces.length) {
7655
+ const linkElement = (span) => {
7656
+ const a = doc.createElement("a");
7657
+ const href = ctx.resolveUrl(span.link, "link");
7658
+ if (href !== null) a.setAttribute("href", href);
7659
+ a.dataset.href = span.link;
7660
+ if (span.linkTitle) a.title = span.linkTitle;
7661
+ a.className = classes("mbe-link", ctx.classes.link);
7662
+ return a;
7663
+ };
7664
+ const runAt = (i) => runs.find((r) => r.from === offsets.get(pieces[i]));
7665
+ const runEnd = (i, run) => {
7666
+ let end = i;
7667
+ while (end < pieces.length && offsets.get(pieces[end]) < run.to) end++;
7668
+ return end;
7669
+ };
7670
+ const linkedRun = (i, run) => {
7659
7671
  const span = pieces[i].span;
7660
- const run = runs.find((r) => r.from === offsets.get(pieces[i]));
7661
- if (run) {
7662
- const html = [];
7663
- const slots = [];
7664
- let slot = null;
7665
- let end = i;
7666
- for (; end < pieces.length && offsets.get(pieces[end]) < run.to; end++) {
7667
- const piece = pieces[end];
7668
- if (isHtml(piece.span)) {
7669
- html.push(piece.span.text);
7670
- slot = null;
7672
+ if (!span.link) return false;
7673
+ for (let j = i + 1, end = runEnd(i, run); j < end; j++) if (!sameLink(pieces[j].span, span)) return false;
7674
+ return true;
7675
+ };
7676
+ const renderRun = (i, run, into, linked) => {
7677
+ const html = [];
7678
+ const slots = [];
7679
+ const end = runEnd(i, run);
7680
+ for (let j = i; j < end; ) {
7681
+ const piece = pieces[j];
7682
+ if (isHtml(piece.span)) {
7683
+ html.push(piece.span.text);
7684
+ j++;
7685
+ continue;
7686
+ }
7687
+ const slot = doc.createDocumentFragment();
7688
+ const stop = (k) => k >= end || isHtml(pieces[k].span);
7689
+ if (linked) for (; !stop(j); j++) slot.appendChild(render2(pieces[j]));
7690
+ else j = renderPieces(j, stop, slot);
7691
+ slots.push(slot);
7692
+ html.push(slotMarker(slots.length - 1));
7693
+ }
7694
+ const view = ctx.view?.("html", html.join(""), "inline");
7695
+ if (!view) return null;
7696
+ fillSlots(view, slots.length, (n) => slots[n], doc);
7697
+ const text = inlineText(sliceInline(inline3, run.from, run.to));
7698
+ into.appendChild(atom(view, classes("mbe-view", "mbe-view-html", ctx.classes.token?.html), text));
7699
+ return end;
7700
+ };
7701
+ const renderPieces = (i, stop, into) => {
7702
+ while (i < pieces.length && !stop(i)) {
7703
+ const span = pieces[i].span;
7704
+ const run = runAt(i);
7705
+ if (run && !linkedRun(i, run)) {
7706
+ const next = renderRun(i, run, into, false);
7707
+ if (next !== null) {
7708
+ i = next;
7671
7709
  continue;
7672
7710
  }
7673
- if (!slot) {
7674
- slot = [];
7675
- slots.push(slot);
7676
- html.push(slotMarker(slots.length - 1));
7677
- }
7678
- slot.push(render2(piece));
7679
7711
  }
7680
- const view = ctx.view?.("html", html.join(""), "inline");
7681
- if (view) {
7682
- fillSlots(view, slots.length, (n) => {
7683
- const fragment = doc.createDocumentFragment();
7684
- fragment.append(...slots[n]);
7685
- return fragment;
7686
- }, doc);
7687
- const text = inlineText(sliceInline(inline3, run.from, run.to));
7688
- parent.appendChild(atom(view, classes("mbe-view", "mbe-view-html", ctx.classes.token?.html), text));
7689
- i = end;
7712
+ if (!span.link) {
7713
+ into.appendChild(render2(pieces[i]));
7714
+ i++;
7690
7715
  continue;
7691
7716
  }
7692
- }
7693
- if (span.link) {
7694
- const a = doc.createElement("a");
7695
- const href = ctx.resolveUrl(span.link, "link");
7696
- if (href !== null) a.setAttribute("href", href);
7697
- a.dataset.href = span.link;
7698
- if (span.linkTitle) a.title = span.linkTitle;
7699
- a.className = classes("mbe-link", ctx.classes.link);
7700
- while (i < pieces.length && sameLink(pieces[i].span, span)) {
7717
+ const a = linkElement(span);
7718
+ const start = i;
7719
+ while (i < pieces.length && !stop(i) && sameLink(pieces[i].span, span)) {
7720
+ const inner = runAt(i);
7721
+ if (inner) {
7722
+ const whole = linkedRun(i, inner);
7723
+ if (!whole && i > start) break;
7724
+ const next = whole ? renderRun(i, inner, a, true) : null;
7725
+ if (next !== null) {
7726
+ i = next;
7727
+ continue;
7728
+ }
7729
+ }
7701
7730
  a.appendChild(render2(pieces[i]));
7702
7731
  i++;
7703
7732
  }
7704
- parent.appendChild(a);
7705
- } else {
7706
- parent.appendChild(render2(pieces[i]));
7707
- i++;
7733
+ into.appendChild(a);
7708
7734
  }
7709
- }
7735
+ return i;
7736
+ };
7737
+ renderPieces(0, () => false, parent);
7710
7738
  }
7711
7739
  function decorate(node, decorations, doc) {
7712
7740
  for (let i = decorations.length - 1; i >= 0; i--) {
@@ -8713,6 +8741,7 @@ var COLOR_VARS = {
8713
8741
  popupBorder: "--mbe-popup-border",
8714
8742
  hover: "--mbe-hover",
8715
8743
  danger: "--mbe-danger",
8744
+ highlight: "--mbe-highlight",
8716
8745
  calloutNote: "--mbe-callout-note",
8717
8746
  calloutTip: "--mbe-callout-tip",
8718
8747
  calloutImportant: "--mbe-callout-important",
@@ -8732,9 +8761,9 @@ function tokenProperties(tokens) {
8732
8761
  const props = /* @__PURE__ */ new Map();
8733
8762
  if (!tokens) return props;
8734
8763
  for (const scheme of ["light", "dark"]) {
8735
- for (const [name, value] of Object.entries(tokens[scheme] ?? {})) {
8736
- const prop = COLOR_VARS[name];
8737
- if (prop && value) props.set(`${prop}-${scheme}`, value);
8764
+ for (const [name, prop] of Object.entries(COLOR_VARS)) {
8765
+ const value = tokens[scheme]?.[name] ?? tokens.colors?.[name];
8766
+ if (value) props.set(`${prop}-${scheme}`, value);
8738
8767
  }
8739
8768
  }
8740
8769
  for (const [name, prop] of Object.entries(SIZE_VARS)) {