@usememos/editor 26.10.17 → 26.10.19

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/dist/style.css CHANGED
@@ -221,6 +221,9 @@
221
221
  margin: 8px 0;
222
222
  vertical-align: bottom;
223
223
  }
224
+ .mbe-root .mbe-image-block .mbe-image {
225
+ margin: 0;
226
+ }
224
227
  .mbe-root .mbe-image-block[data-align=left] {
225
228
  text-align: left;
226
229
  }
@@ -314,6 +317,12 @@
314
317
  :is(.mbe-root, .mbe-flow) > .mbe-view-block > * {
315
318
  margin-block: 0;
316
319
  }
320
+ .mbe-root > .mbe-block-view {
321
+ overflow: visible;
322
+ }
323
+ .mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) {
324
+ cursor: pointer;
325
+ }
317
326
  .mbe-root .mbe-view-block.mbe-view-html {
318
327
  white-space: normal;
319
328
  }
@@ -517,9 +526,9 @@
517
526
  color: inherit;
518
527
  background: none;
519
528
  border: none;
520
- padding: 4px 7px;
521
- min-width: 32px;
522
- min-height: 32px;
529
+ padding: 2px 6px;
530
+ min-width: 28px;
531
+ min-height: 28px;
523
532
  display: inline-flex;
524
533
  align-items: center;
525
534
  justify-content: center;
@@ -530,20 +539,20 @@
530
539
  display: none;
531
540
  }
532
541
  .mbe-tool .mbe-icon {
533
- width: 18px;
534
- height: 18px;
542
+ width: 16px;
543
+ height: 16px;
535
544
  }
536
545
  .mbe-tool-menu {
537
546
  gap: 1px;
538
- padding-inline: 7px 4px;
547
+ padding-inline: 6px 3px;
539
548
  }
540
549
  .mbe-tool-icon,
541
550
  .mbe-tool-chevron {
542
551
  display: inline-flex;
543
552
  }
544
553
  .mbe-tool .mbe-tool-chevron .mbe-icon {
545
- width: 13px;
546
- height: 13px;
554
+ width: 12px;
555
+ height: 12px;
547
556
  }
548
557
  .mbe-tool.mbe-active {
549
558
  background: Highlight;
@@ -552,8 +561,8 @@
552
561
  .mbe-tool-sep {
553
562
  flex: none;
554
563
  width: 1px;
555
- height: 18px;
556
- margin: 0 3px;
564
+ height: 16px;
565
+ margin: 0 2px;
557
566
  background: GrayText;
558
567
  }
559
568
  .mbe-menu-check {
@@ -655,8 +664,10 @@
655
664
  .mbe-popover-input {
656
665
  font: inherit;
657
666
  color: inherit;
667
+ box-sizing: border-box;
668
+ height: 28px;
658
669
  width: 260px;
659
- padding: 5px 8px;
670
+ padding: 0 8px;
660
671
  }
661
672
  .mbe-block-handle {
662
673
  position: fixed;
@@ -812,6 +823,9 @@
812
823
  .mbe-root.mbe-readonly .mbe-footnote-ref {
813
824
  cursor: pointer;
814
825
  }
826
+ .mbe-root.mbe-readonly [data-mbe-mention]:hover {
827
+ text-decoration: underline;
828
+ }
815
829
  :is(.mbe-root, .mbe-flow) > .mbe-footnote {
816
830
  font-size: 0.875em;
817
831
  color: var(--mbe-muted);
@@ -1116,7 +1130,7 @@
1116
1130
  text-decoration: underline;
1117
1131
  }
1118
1132
  .mbe-tool {
1119
- border-radius: 7px;
1133
+ border-radius: 6px;
1120
1134
  }
1121
1135
  .mbe-tool:hover,
1122
1136
  .mbe-tool.mbe-open {
package/dist/theme.css CHANGED
@@ -110,6 +110,8 @@
110
110
  /* Footnotes, rendered: a reference is a small raised label, a definition a small muted note. */
111
111
  .mbe-root .mbe-footnote-ref { font-size: 0.75em; line-height: 0; color: var(--mbe-link); }
112
112
  .mbe-root.mbe-readonly .mbe-footnote-ref { cursor: pointer; }
113
+ /* A mention with an onClick (tags.ts), read-only: underlined on hover, as a name that leads somewhere. */
114
+ .mbe-root.mbe-readonly [data-mbe-mention]:hover { text-decoration: underline; }
113
115
  :is(.mbe-root, .mbe-flow) > .mbe-footnote { font-size: 0.875em; color: var(--mbe-muted); }
114
116
  .mbe-root .mbe-footnote-label { margin-inline-end: 0.375em; color: var(--mbe-link); }
115
117
 
@@ -283,7 +285,7 @@
283
285
  .mbe-tool-delete:hover { color: var(--mbe-danger); }
284
286
  .mbe-link-card-url { font-size: 13px; color: var(--mbe-link); text-decoration: none; }
285
287
  .mbe-link-card-url:hover { text-decoration: underline; }
286
- .mbe-tool { border-radius: 7px; }
288
+ .mbe-tool { border-radius: 6px; }
287
289
  .mbe-tool:hover,
288
290
  .mbe-tool.mbe-open { background: var(--mbe-hover); }
289
291
  .mbe-tool.mbe-active { color: var(--mbe-accent); background: var(--mbe-hover); }
@@ -15,6 +15,15 @@ export declare function deleteBackward(state: EditorState): EditorState;
15
15
  export declare function deleteForward(state: EditorState): EditorState;
16
16
  /** Inserts text at the selection with the current format. "\n" is a hard line break. */
17
17
  export declare function insertText(state: EditorState, text: string): EditorState;
18
+ /**
19
+ * Inserts a token such as `#tag` at the selection's focus, spaced as a word
20
+ * among words: a space before it unless the caret is at the start of the
21
+ * text or after whitespace, and a space after it unless one follows, which
22
+ * the caret moves past; either way the caret ends after that space. Selected
23
+ * text is kept, and the token takes the format at the caret as typed text
24
+ * would: plain in a code or raw block.
25
+ */
26
+ export declare function insertToken(state: EditorState, token: string): EditorState;
18
27
  /**
19
28
  * ArrowDown on the last line of the last block, or ArrowUp on the first line
20
29
  * of the first block, when that block is a code block, table or divider, or
@@ -221,6 +221,15 @@ export interface EditorCommands {
221
221
  to: Position;
222
222
  };
223
223
  }): boolean;
224
+ /**
225
+ * Inserts a token such as `#tag` at the caret as a word of its own: with a
226
+ * space before it unless the caret is at the start of the text or after
227
+ * whitespace, and a space after it, reusing one that is already there; the
228
+ * caret ends after that space. Selected text is kept. The token is plain
229
+ * text in a code block or source mode, as typed text is. Does nothing, and
230
+ * returns false, while an input method is composing.
231
+ */
232
+ insertToken(text: string): boolean;
224
233
  insertMarkdown(markdown: string, options?: InsertMarkdownOptions): boolean;
225
234
  insertTable(attrs?: {
226
235
  rows?: number;
@@ -415,7 +424,7 @@ export declare class Editor {
415
424
  on<K extends keyof EditorEvents>(type: K, handler: (payload: EditorEvents[K]) => void): () => void;
416
425
  private emit;
417
426
  private applyRuntime;
418
- /** Sets the theme. Returns true when blocks must re-render for new classes. */
427
+ /** Sets the theme. Returns true when every block must re-render, for new classes. */
419
428
  private applyTheme;
420
429
  /** Applies a command (a pure function of the state), records history, re-renders, and emits events. */
421
430
  run(command: (state: EditorState) => EditorState | null, kind?: ChangeKind): boolean;
@@ -483,15 +492,29 @@ export declare class Editor {
483
492
  * loading. Waits for an input method to finish composing.
484
493
  */
485
494
  redecorate(): void;
495
+ /**
496
+ * Renders every view (`Extension.views`) again, or those of one syntax
497
+ * (`'math'`, `'code:mermaid'`), for when what a view shows depends on
498
+ * something besides its source that changed: a renderer that finished
499
+ * loading, or the font a diagram measured its text with. A view that
500
+ * failed is tried again. Only the blocks that show those views render
501
+ * again. The editor calls it itself when the theme's font tokens change.
502
+ * Waits for an input method to finish composing.
503
+ */
504
+ refreshViews(syntax?: string): void;
505
+ /** Renders the given blocks again now, or when the composition in progress settles. */
506
+ private renderBlocksAgain;
486
507
  /**
487
508
  * Views that returned a promise settled: their blocks render again on the
488
509
  * next frame, or when a composition settles, since rendering would end it.
489
510
  */
490
511
  private viewsSettled;
491
- /** A block rendered by a view (a formula block, a diagram), or null. */
512
+ /** A block rendered by a view (a formula block, a diagram, a block an extension's block view draws), or null. */
492
513
  private viewBlock;
493
514
  /** Re-renders if the views that show their source changed: after the selection, focus or editability changes. */
494
515
  private refreshRevealed;
516
+ /** Whether an extension's block view draws `block` (see `BlockView`). */
517
+ private readonly hosted;
495
518
  private onMouseUp;
496
519
  /**
497
520
  * When an arrow key at the edge of a block would move past a rendered raw
@@ -2,7 +2,7 @@ import type { Decoration, Decorator } from './decorations.js';
2
2
  import type { Editor } from './editor.js';
3
3
  import type { SuggestionConfig } from './extensions/suggestion.js';
4
4
  import type { MarkdownSyntax } from './markdown/options.js';
5
- import type { CodeBlock, Position } from './model.js';
5
+ import type { Block, CodeBlock, Position } from './model.js';
6
6
  import type { ThemeClasses } from './theme.js';
7
7
  export interface ExtensionContext {
8
8
  readonly editor: Editor;
@@ -16,14 +16,40 @@ export type KeyHandler = (ctx: ExtensionContext) => boolean;
16
16
  * Renders the Markdown source of a token (`kind` is `inline`, `element` a
17
17
  * span), a raw block, or the code of a code block (`block`, a div) into
18
18
  * `element`. Returns false, or throws, to show the source instead. Results
19
- * are cached by source, so the output must depend on nothing else.
19
+ * are cached by source, so the output must depend on nothing else; what a
20
+ * view reads from the editor instead (its font, for a layout that measures
21
+ * text) is current again after `editor.refreshViews()`, which the editor
22
+ * calls itself when the theme's font tokens change.
23
+ *
24
+ * `element` is not in the page yet, so it has no layout. A view that
25
+ * measures text uses the editor: `getComputedStyle(ctx.editor.root)` gives
26
+ * the font the view's text will have, for a canvas `measureText`.
20
27
  *
21
28
  * A view that finishes later (a diagram, a renderer that loads on demand)
22
29
  * fills in what it can at once and returns a promise: the editor shows that
23
30
  * meanwhile, and the finished element when the promise resolves. A promise
24
31
  * that rejects or resolves to false shows the source.
25
32
  */
26
- export type SyntaxView = (source: string, element: HTMLElement, kind: 'inline' | 'block') => void | false | PromiseLike<void | false>;
33
+ export type SyntaxView = (source: string, element: HTMLElement, kind: 'inline' | 'block', ctx: ExtensionContext) => void | false | PromiseLike<void | false>;
34
+ /**
35
+ * Draws whole blocks the app wants to show its own way, such as a paragraph
36
+ * that is one bare link shown as a preview card with data from a server.
37
+ * `match` picks the blocks; `render` draws one into `element`, a div the
38
+ * editor owns and keeps while the block is unchanged: typing elsewhere, a
39
+ * new selection or `redecorate()` never call `render` again, so app UI
40
+ * mounted into the element (a React portal) stays. The editor calls `render`
41
+ * again for a block whose content changed, and for a new block. It calls the
42
+ * function `render` returned when it lets the element go: the block was
43
+ * removed or changed, `editor.refreshViews()` ran, or the editor was
44
+ * destroyed. `render` returns false to leave the block as the editor draws
45
+ * it. While editing, the block shows as the editor draws it, editable, when
46
+ * the selection is in it, as a rendered formula shows its source; read-only
47
+ * always shows the view.
48
+ */
49
+ export interface BlockView {
50
+ readonly match: (block: Block) => boolean;
51
+ readonly render: (block: Block, element: HTMLElement, ctx: ExtensionContext) => void | false | (() => void);
52
+ }
27
53
  /** A code block's code, for decorations such as syntax highlighting. */
28
54
  export interface CodeDecorationContext {
29
55
  readonly block: CodeBlock;
@@ -78,6 +104,12 @@ export interface Extension {
78
104
  * same. Read-only always shows the rendered view.
79
105
  */
80
106
  readonly views?: Readonly<Record<string, SyntaxView>>;
107
+ /**
108
+ * Draws the blocks `match` picks with `render`, in place of the editor's
109
+ * own rendering; see `BlockView`. `editor.refreshViews(name)`, with this
110
+ * extension's name, draws them again.
111
+ */
112
+ readonly blockView?: BlockView;
81
113
  /**
82
114
  * Theme classes for the elements this extension cares about, such as
83
115
  * `token.math`. The editor's `theme` wins for the same key, and an earlier
@@ -1,4 +1,5 @@
1
1
  import { type TextRange } from '../decorations.js';
2
+ import type { Editor } from '../editor.js';
2
3
  import type { Extension } from '../extension.js';
3
4
  /** Finds tags or mentions in `text[from, to)`. `text` is the whole container, so a scanner can look before `from`. */
4
5
  export type TagScanner = (text: string, from: number, to: number) => Iterable<TextRange>;
@@ -13,6 +14,20 @@ export interface TagsOptions {
13
14
  readonly attributes?: Readonly<Record<string, string>> | ((match: string) => Readonly<Record<string, string>> | undefined);
14
15
  /** Finds matches in a run of text. Default `scanTags` or `scanMentions`. */
15
16
  readonly scan?: TagScanner;
17
+ /**
18
+ * Called when a match is clicked to be followed, as `onLinkClick` is for a
19
+ * link: a plain click while read-only, Mod+click while editing. `match` is
20
+ * the text as written (`#work/planning`). Return true to handle it: the
21
+ * click then goes no further, neither to the browser nor to the page's
22
+ * own listeners. With this option, read-only matches show a pointer
23
+ * cursor, and mentions underline on hover; the editor marks each match with
24
+ * `data-mbe-tag` or `data-mbe-mention`.
25
+ */
26
+ readonly onClick?: (ctx: {
27
+ readonly editor: Editor;
28
+ readonly match: string;
29
+ readonly event: MouseEvent;
30
+ }) => boolean | void;
16
31
  }
17
32
  /**
18
33
  * The default tag grammar, memos': `#` and one or more segments separated
@@ -12,7 +12,7 @@ export type { EditorMode } from './sourceMode.js';
12
12
  export type { MarkdownOptions } from './markdown/options.js';
13
13
  export { docToHtml } from './clipboard/toHtml.js';
14
14
  export { htmlToDoc } from './clipboard/fromHtml.js';
15
- export type { CodeDecorationContext, Extension, ExtensionContext, KeyHandler, SyntaxView } from './extension.js';
15
+ export type { BlockView, CodeDecorationContext, Extension, ExtensionContext, KeyHandler, SyntaxView } from './extension.js';
16
16
  export type { ColorScheme, Theme, ThemeClasses, ThemeColors, ThemeTokens } from './theme.js';
17
17
  export { defaultKeymap } from './extensions/keymap.js';
18
18
  export { math, type MathOptions } from './extensions/math.js';
@@ -88,6 +88,8 @@ export interface Theme {
88
88
  }
89
89
  /** Merges class maps; a later map wins for the same key, and `heading` and `token` merge per entry. */
90
90
  export declare function mergeClasses(maps: readonly (ThemeClasses | undefined)[]): ThemeClasses;
91
+ /** The token properties that change how text measures: a view that laid out text (a diagram) renders again when one changes. */
92
+ export declare const FONT_PROPERTIES: readonly string[];
91
93
  /**
92
94
  * The custom properties for `tokens`. Colors go in `--mbe-<name>-light` and
93
95
  * `--mbe-<name>-dark`, which the stylesheet picks between with `light-dark()`.
@@ -1,5 +1,5 @@
1
1
  import { type Block, type Cell, type CalloutType, type CodeBlock, type Doc, type Inline, type Position } from '../model.js';
2
- import type { SyntaxView } from '../extension.js';
2
+ import type { BlockView, ExtensionContext, SyntaxView } from '../extension.js';
3
3
  import type { Labels } from '../labels.js';
4
4
  import type { IconName } from '../icons.js';
5
5
  import type { ThemeClasses } from '../theme.js';
@@ -34,6 +34,8 @@ export interface RenderContext {
34
34
  readonly views?: ReadonlyMap<string, SyntaxView>;
35
35
  /** A token or raw block of an extension syntax, rendered; null shows its source. */
36
36
  readonly view?: (syntax: string, source: string, kind: 'inline' | 'block') => HTMLElement | null;
37
+ /** The element an extension's block view draws a block of the document into, or null for none (see `BlockView`). */
38
+ readonly blockView?: (block: Block) => HTMLElement | null;
37
39
  /** The document's Markdown options, for the Markdown inside an HTML section. */
38
40
  readonly markdown?: MarkdownOptions;
39
41
  /** Theme classes for rendered elements. */
@@ -103,6 +105,19 @@ export declare class BlockRenderer {
103
105
  widgetsAfter?: (blockId: string) => readonly HTMLElement[] | undefined;
104
106
  /** Views of extension syntaxes, by syntax name. */
105
107
  views?: ReadonlyMap<string, SyntaxView>;
108
+ /** What a view gets besides its source: the editor. Set with `views`. */
109
+ viewContext?: ExtensionContext;
110
+ /** Block views of extensions, with the extension's name, in extension order. */
111
+ blockViews: readonly {
112
+ readonly name: string;
113
+ readonly view: BlockView;
114
+ }[];
115
+ /**
116
+ * Per block drawn by a block view, the element the view drew into (null
117
+ * when every matching view declined), kept while the block object is the
118
+ * same, and the function to call when the element is let go.
119
+ */
120
+ private readonly hosted;
106
121
  /** The document's Markdown options, for the Markdown inside an HTML section. */
107
122
  markdown?: MarkdownOptions;
108
123
  /** Theme classes for rendered elements. Set before a full render. */
@@ -128,6 +143,8 @@ export declare class BlockRenderer {
128
143
  private readonly pendingViews;
129
144
  /** Views whose promise rejected or resolved to false: their source shows. */
130
145
  private readonly failedViews;
146
+ /** Per cached or failed view, the blocks that showed it, to render again when `dropViews` forgets it. */
147
+ private readonly viewBlocks;
131
148
  /** The block being rendered, so a pending view knows which blocks to render again. */
132
149
  private renderingBlock;
133
150
  /** During a render, the loaded images of the block elements it replaces, by `src`. */
@@ -135,11 +152,30 @@ export declare class BlockRenderer {
135
152
  constructor(root: HTMLElement, labels: Labels);
136
153
  private context;
137
154
  private renderView;
155
+ /** Whether a block view draws `block`: its blocks show their editable form only while the selection is in them. */
156
+ isHosted(block: Block): boolean;
157
+ /**
158
+ * The element a block view drew `block` into: the one from the last render
159
+ * while the block object is the same, else a fresh one from the first
160
+ * matching view that does not decline. Null when none draws it.
161
+ */
162
+ private renderBlockView;
163
+ private disposeHosted;
164
+ /** Lets every block view's element go: on destroy. */
165
+ destroy(): void;
166
+ private noteViewBlock;
138
167
  /**
139
168
  * A view that renders later (a diagram, a lazily loaded renderer) shows what
140
169
  * it filled in at once, and its blocks render again when it settles.
141
170
  */
142
171
  private awaitView;
172
+ /**
173
+ * Forgets rendered and failed views, of one syntax or all, so the next
174
+ * render runs the view again: for when what it shows depends on something
175
+ * besides its source that changed, such as the editor's font. Returns the
176
+ * rendered blocks that showed them, for the caller to render again.
177
+ */
178
+ dropViews(syntax?: string): string[];
143
179
  /**
144
180
  * `force` re-renders every block. `lazy` (with a full render) lets blocks
145
181
  * skip layout while off screen; use it only when loading a document.
@@ -1,13 +1,18 @@
1
1
  import type { SyntaxView } from '../extension.js';
2
- import { type CodeBlock, type EditorState, type Position, type RawBlock } from '../model.js';
2
+ import { type Block, type EditorState, type Position } from '../model.js';
3
3
  import { type BlockReveal } from './dom.js';
4
- /** Per block, the views that show their source. Empty when `active` is false (read-only or unfocused). */
5
- export declare function revealedViews(state: EditorState, views: ReadonlyMap<string, SyntaxView>, active: boolean): Map<string, BlockReveal>;
4
+ /**
5
+ * Per block, the views that show their source. Empty when `active` is false
6
+ * (read-only or unfocused). `hosted` says whether an extension's block view
7
+ * draws a block, which then shows its editable form instead.
8
+ */
9
+ export declare function revealedViews(state: EditorState, views: ReadonlyMap<string, SyntaxView>, active: boolean, hosted?: (block: Block) => boolean): Map<string, BlockReveal>;
6
10
  export declare function sameRevealed(a: ReadonlyMap<string, BlockReveal>, b: ReadonlyMap<string, BlockReveal>): boolean;
7
11
  /**
8
12
  * Where the caret goes in a rendered block's source. A raw block: past its
9
13
  * opening line going forward (such as a `$$` fence), at the end of its
10
14
  * next-to-last line going back or on a click. A code block holds only the
11
- * code, and raw Markdown has no fence: their start or their end.
15
+ * code, and raw Markdown has no fence: their start or their end. So does any
16
+ * other block drawn by a block view; a table, its first or last cell.
12
17
  */
13
- export declare function viewEntry(block: RawBlock | CodeBlock, forward: boolean): Position;
18
+ export declare function viewEntry(block: Block, forward: boolean): Position;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usememos/editor",
3
- "version": "26.10.17",
3
+ "version": "26.10.19",
4
4
  "description": "A block editor for Markdown. Zero runtime dependencies, with an optional React component.",
5
5
  "keywords": [
6
6
  "markdown",