@usememos/editor 26.10.19 → 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 +19 -1
- package/dist/base.css +11 -3
- package/dist/index.js +83 -22
- package/dist/index.js.map +2 -2
- package/dist/style.css +16 -6
- package/dist/theme.css +0 -1
- package/dist/types/extension.d.ts +8 -0
- package/dist/types/model.d.ts +7 -0
- package/dist/types/view/dom.d.ts +23 -5
- package/package.json +1 -1
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,11 +265,27 @@ 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
|
|
269
287
|
|
|
270
|
-
`style.css` is two stylesheets: `base.css` holds the layout the editor needs and all spacing between blocks, and `theme.css` the default look. The default look is quiet and compact, like a rendered Markdown view: a 16px body with 24px lines, 8px between blocks, headings close to the body size, muted quotes and list markers, code blocks on a light panel with no border, and tables as a light grid with a shaded header row. Editing and read-only look the same; only the cursor on links and checkboxes changes. It has no border or background of its own, so it fits inside your own card, comment box or chat input.
|
|
288
|
+
`style.css` is two stylesheets: `base.css` holds the layout the editor needs and all spacing between blocks, and `theme.css` the default look. The default look is quiet and compact, like a rendered Markdown view: a 16px body with 24px lines, 8px between blocks, headings close to the body size, muted quotes and list markers, code blocks on a light panel with no border whose long lines scroll sideways rather than wrap, and tables as a light grid with a shaded header row and columns sized to their content. Editing and read-only look the same; only the cursor on links and checkboxes changes. It has no border or background of its own, so it fits inside your own card, comment box or chat input.
|
|
271
289
|
|
|
272
290
|
Every color, font and size in `theme.css` is a CSS custom property (`--mbe-text`, `--mbe-accent`, `--mbe-font`, …). Set them with the `theme` option's tokens, with a color set for light and one for dark; colors you leave out keep the defaults:
|
|
273
291
|
|
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; }
|
|
@@ -236,7 +238,8 @@
|
|
|
236
238
|
padding: 0;
|
|
237
239
|
width: 12em;
|
|
238
240
|
}
|
|
239
|
-
|
|
241
|
+
/* Code keeps its lines and scrolls sideways, as GitHub shows it; raw source wraps. */
|
|
242
|
+
:is(.mbe-root, .mbe-flow) > .mbe-code pre { white-space: pre; overflow-x: auto; tab-size: 2; }
|
|
240
243
|
:is(.mbe-root, .mbe-flow) > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
|
|
241
244
|
/* Source mode: the document as plain text in the editor's font. */
|
|
242
245
|
.mbe-root > .mbe-source { white-space: pre-wrap; tab-size: 4; overflow-wrap: anywhere; }
|
|
@@ -250,6 +253,8 @@
|
|
|
250
253
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
|
|
251
254
|
/* A block an extension's block view draws (see Extension.blockView): the app owns what is inside, so nothing is clipped. */
|
|
252
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; }
|
|
253
258
|
/* Tags and mentions with an onClick, read-only: clickable, as links are. */
|
|
254
259
|
.mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
|
|
255
260
|
/* Rendered HTML: line breaks between its tags are not text. The Markdown
|
|
@@ -276,16 +281,19 @@
|
|
|
276
281
|
|
|
277
282
|
/* Table */
|
|
278
283
|
:is(.mbe-root, .mbe-flow) > .mbe-table { overflow-x: auto; }
|
|
279
|
-
/* Columns fit their content and wrap at the editor's width
|
|
284
|
+
/* Columns fit their content and wrap at the editor's width, as GitHub lays
|
|
285
|
+
tables out; wider tables scroll. No column has a minimum width, so a long
|
|
286
|
+
column is not squeezed into wrapping by short ones. */
|
|
280
287
|
:is(.mbe-root, .mbe-flow) > .mbe-table table { border-collapse: separate; border-spacing: 0; }
|
|
281
288
|
:is(.mbe-root, .mbe-flow) > .mbe-table th,
|
|
282
289
|
:is(.mbe-root, .mbe-flow) > .mbe-table td {
|
|
283
290
|
padding: 4px 8px;
|
|
284
|
-
min-width: 4em;
|
|
285
291
|
vertical-align: top;
|
|
286
292
|
text-align: start;
|
|
287
293
|
outline: none;
|
|
288
294
|
}
|
|
295
|
+
/* An empty cell keeps room to click into while editing. */
|
|
296
|
+
.mbe-root:not(.mbe-readonly) > .mbe-table :is(th, td).mbe-empty-content { min-width: 4em; }
|
|
289
297
|
|
|
290
298
|
/* Blocks inside collapsible sections, after the block rules so it wins over their margins. */
|
|
291
299
|
:is(.mbe-root, .mbe-flow) > [data-in-section] { margin-inline-start: calc(var(--mbe-section-depth, 0) * var(--mbe-indent-size)); }
|
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
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
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)));
|
|
@@ -7567,10 +7581,29 @@ function fill(doc) {
|
|
|
7567
7581
|
return br;
|
|
7568
7582
|
}
|
|
7569
7583
|
var VOID_TAGS = /* @__PURE__ */ new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"]);
|
|
7584
|
+
var HTML_PIECE = /<!--[\s\S]*?-->|<[^<>]*>/g;
|
|
7585
|
+
function splitHtmlSpans(inline3) {
|
|
7586
|
+
if (!inline3.some((s) => s.marks.includes("html"))) return inline3;
|
|
7587
|
+
const out = [];
|
|
7588
|
+
for (const span of normalizeInline(inline3)) {
|
|
7589
|
+
if (!span.marks.includes("html")) {
|
|
7590
|
+
out.push(span);
|
|
7591
|
+
continue;
|
|
7592
|
+
}
|
|
7593
|
+
let last = 0;
|
|
7594
|
+
for (const m of span.text.matchAll(HTML_PIECE)) {
|
|
7595
|
+
if (m.index > last) out.push({ ...span, text: span.text.slice(last, m.index) });
|
|
7596
|
+
out.push({ ...span, text: m[0] });
|
|
7597
|
+
last = m.index + m[0].length;
|
|
7598
|
+
}
|
|
7599
|
+
if (last < span.text.length) out.push({ ...span, text: span.text.slice(last) });
|
|
7600
|
+
}
|
|
7601
|
+
return out;
|
|
7602
|
+
}
|
|
7570
7603
|
function htmlRuns(inline3) {
|
|
7571
7604
|
const spans = [];
|
|
7572
7605
|
let pos = 0;
|
|
7573
|
-
for (const span of inline3) {
|
|
7606
|
+
for (const span of splitHtmlSpans(inline3)) {
|
|
7574
7607
|
spans.push({ span, from: pos, to: pos + span.text.length });
|
|
7575
7608
|
pos += span.text.length;
|
|
7576
7609
|
}
|
|
@@ -7608,6 +7641,7 @@ function renderInline(inline3, parent, ctx, decorations, reveal, cell) {
|
|
|
7608
7641
|
const doc = ctx.doc;
|
|
7609
7642
|
const views = ctx.views;
|
|
7610
7643
|
const viewed = views ? (span, from) => !!span.token && views.has(span.token) && !reveal?.has(tokenKey(cell, from)) : void 0;
|
|
7644
|
+
if (views?.has("html")) inline3 = splitHtmlSpans(inline3);
|
|
7611
7645
|
const runs = views?.has("html") && inline3.some((s) => s.marks.includes("html")) ? htmlRuns(inline3).filter((run) => !reveal?.has(tokenKey(cell, run.from))) : [];
|
|
7612
7646
|
const inRun = (from) => runs.some((run) => from >= run.from && from < run.to);
|
|
7613
7647
|
const isHtml = (span) => span.marks.includes("html");
|
|
@@ -7795,7 +7829,9 @@ var BlockRenderer = class {
|
|
|
7795
7829
|
/**
|
|
7796
7830
|
* Per block drawn by a block view, the element the view drew into (null
|
|
7797
7831
|
* when every matching view declined), kept while the block object is the
|
|
7798
|
-
* 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.
|
|
7799
7835
|
*/
|
|
7800
7836
|
hosted = /* @__PURE__ */ new Map();
|
|
7801
7837
|
/** The document's Markdown options, for the Markdown inside an HTML section. */
|
|
@@ -7889,13 +7925,15 @@ var BlockRenderer = class {
|
|
|
7889
7925
|
return this.blockViews.some(({ view }) => view.match(block3));
|
|
7890
7926
|
}
|
|
7891
7927
|
/**
|
|
7892
|
-
* The element a block view drew `block` into
|
|
7893
|
-
*
|
|
7894
|
-
* matching view that does not decline.
|
|
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.
|
|
7895
7933
|
*/
|
|
7896
7934
|
renderBlockView(block3) {
|
|
7897
7935
|
const prev = this.hosted.get(block3.id);
|
|
7898
|
-
if (prev && prev.block === block3) return prev.el;
|
|
7936
|
+
if (prev && prev.block === block3) return prev.after ? null : prev.el;
|
|
7899
7937
|
if (prev) this.disposeHosted(block3.id);
|
|
7900
7938
|
const ctx = this.viewContext;
|
|
7901
7939
|
if (!ctx) return null;
|
|
@@ -7911,12 +7949,27 @@ var BlockRenderer = class {
|
|
|
7911
7949
|
result = false;
|
|
7912
7950
|
}
|
|
7913
7951
|
if (result === false) continue;
|
|
7914
|
-
|
|
7915
|
-
|
|
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;
|
|
7916
7963
|
}
|
|
7917
7964
|
if (matched !== null) this.hosted.set(block3.id, { block: block3, name: matched, el: null });
|
|
7918
7965
|
return null;
|
|
7919
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
|
+
}
|
|
7920
7973
|
disposeHosted(id) {
|
|
7921
7974
|
const entry = this.hosted.get(id);
|
|
7922
7975
|
if (!entry) return;
|
|
@@ -8021,8 +8074,10 @@ var BlockRenderer = class {
|
|
|
8021
8074
|
this.loadedImages.clear();
|
|
8022
8075
|
for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
|
|
8023
8076
|
const nodes = [];
|
|
8024
|
-
for (const [id, { el }] of next) {
|
|
8077
|
+
for (const [id, { block: block3, el, reveal }] of next) {
|
|
8025
8078
|
nodes.push(el);
|
|
8079
|
+
const after = this.afterView(block3, reveal);
|
|
8080
|
+
if (after) nodes.push(after);
|
|
8026
8081
|
const widgets = this.widgetsAfter?.(id);
|
|
8027
8082
|
if (widgets) nodes.push(...widgets);
|
|
8028
8083
|
}
|
|
@@ -8907,6 +8962,7 @@ var ALIGN_CHOICES = [
|
|
|
8907
8962
|
{ id: "alignRight", align: "right", image: "imageAlignRight" }
|
|
8908
8963
|
];
|
|
8909
8964
|
var SLASH_CHARS = ["/", "\uFF0F"];
|
|
8965
|
+
var APP_UI = ".mbe-widget, .mbe-block-after";
|
|
8910
8966
|
var CARET_KEY = /^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/;
|
|
8911
8967
|
var COMPOSITION_INPUT = /* @__PURE__ */ new Set(["insertCompositionText", "deleteCompositionText", "insertFromComposition", "deleteByComposition"]);
|
|
8912
8968
|
var Editor = class {
|
|
@@ -9302,7 +9358,7 @@ var Editor = class {
|
|
|
9302
9358
|
}
|
|
9303
9359
|
hasFocus() {
|
|
9304
9360
|
const active = this.root.ownerDocument.activeElement;
|
|
9305
|
-
return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(
|
|
9361
|
+
return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(APP_UI);
|
|
9306
9362
|
}
|
|
9307
9363
|
// -------------------------------------------------------------------------
|
|
9308
9364
|
// Widgets
|
|
@@ -10265,7 +10321,7 @@ var Editor = class {
|
|
|
10265
10321
|
// Input
|
|
10266
10322
|
/** Events from the code-language input and other form controls inside the root are not editor input. */
|
|
10267
10323
|
fromControl(e) {
|
|
10268
|
-
return e.target instanceof HTMLInputElement || !!e.target?.closest?.(
|
|
10324
|
+
return e.target instanceof HTMLInputElement || !!e.target?.closest?.(APP_UI);
|
|
10269
10325
|
}
|
|
10270
10326
|
onBeforeInput(e) {
|
|
10271
10327
|
if (this.fromControl(e)) return;
|
|
@@ -10921,8 +10977,12 @@ var Editor = class {
|
|
|
10921
10977
|
this.refreshSelection();
|
|
10922
10978
|
if (isCollapsed(this.state.selection) || !e.clipboardData) return;
|
|
10923
10979
|
e.preventDefault();
|
|
10924
|
-
const
|
|
10925
|
-
|
|
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);
|
|
10926
10986
|
if (cut && !this.readOnly) this.run(deleteSelection);
|
|
10927
10987
|
}
|
|
10928
10988
|
onPaste(e) {
|
|
@@ -11682,9 +11742,9 @@ function blockName(block3) {
|
|
|
11682
11742
|
return block3.type;
|
|
11683
11743
|
}
|
|
11684
11744
|
}
|
|
11685
|
-
function writeClipboard(data, doc,
|
|
11686
|
-
data.setData("text/plain",
|
|
11687
|
-
data.setData("text/html", `<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(
|
|
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>`);
|
|
11688
11748
|
}
|
|
11689
11749
|
function sameBlocks(a, b) {
|
|
11690
11750
|
return a.blocks.length === b.blocks.length && a.blocks.every((block3, i) => block3 === b.blocks[i]);
|
|
@@ -12141,6 +12201,7 @@ export {
|
|
|
12141
12201
|
htmlToDoc,
|
|
12142
12202
|
imageBlockOffset,
|
|
12143
12203
|
inlineLength,
|
|
12204
|
+
inlineLines,
|
|
12144
12205
|
inlineText,
|
|
12145
12206
|
insertInline,
|
|
12146
12207
|
isCollapsed,
|