@memorysmithapp/markdown-renderer 0.6.0 → 0.7.0

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
@@ -86,7 +86,7 @@ A note the host makes `editable` carries a button at its top right that switches
86
86
  suggest={(typed) => namesOfNotesLike(typed)} // what [[ offers
87
87
  attach={(file, { name, description }, progress) => keepInNotebook(file, name, description, progress)} // the name it is kept under
88
88
  files={() => filesOfTheNotebook()} // what the attach dialog lists: { name, mimeType, bytes, url, description }
89
- modeButton="label" // the icon and its word; "icon", the default, the icon alone
89
+ buttons="responsive" // the corner's buttons and the dialog's Copy: their word on a wide note; "label" always; "icon", the default, never
90
90
  />
91
91
  ```
92
92
 
@@ -3,7 +3,26 @@
3
3
  * shows the note's Markdown in a dialog (#63), and one that copies it. Apart from the editor, so a
4
4
  * note that is only read offers them without loading it.
5
5
  */
6
- /** Show Markdown and Copy Markdown; `text` answers the bytes when a button is used. */
7
- export declare function NoteActions({ text }: {
6
+ import { type ReactNode } from 'react';
7
+ /**
8
+ * How the buttons a note draws around its text show their word (#96): `icon`, the icon alone;
9
+ * `label`, the icon and the word; `responsive`, the word beside the icon on a note at least
10
+ * `--markdown-button-words-from` wide and hidden on a narrower one. Wherever the word may be
11
+ * hidden it is the button's name.
12
+ */
13
+ export type NoteButtons = 'icon' | 'label' | 'responsive';
14
+ /** A button's icon and, unless it is drawn alone, its word, which the stylesheet may hide. */
15
+ export declare function ButtonFace({ icon, word, display, wordClass }: {
16
+ icon: ReactNode;
17
+ word: string;
18
+ display: NoteButtons;
19
+ wordClass?: string;
20
+ }): import("react").JSX.Element;
21
+ /**
22
+ * Show Markdown and Copy Markdown; `text` answers the bytes when a button is used, and `display`
23
+ * how they and the dialog's Copy show their word.
24
+ */
25
+ export declare function NoteActions({ text, display }: {
8
26
  text: () => string;
27
+ display?: NoteButtons;
9
28
  }): import("react").JSX.Element;
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  /**
3
3
  * What stands beside the switch between reading and editing, in both modes (#76): a button that
4
4
  * shows the note's Markdown in a dialog (#63), and one that copies it. Apart from the editor, so a
@@ -7,6 +7,10 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
7
7
  import { useEffect, useRef, useState } from 'react';
8
8
  import { useHost } from './host.js';
9
9
  import { ICON_CHECK, ICON_CLOSE, ICON_COPY, ICON_MARKDOWN } from './mode-icons.js';
10
+ /** A button's icon and, unless it is drawn alone, its word, which the stylesheet may hide. */
11
+ export function ButtonFace({ icon, word, display, wordClass }) {
12
+ return (_jsxs(_Fragment, { children: [icon, display !== 'icon' && _jsx("span", { className: wordClass ? `note-button-word ${wordClass}` : 'note-button-word', children: word })] }));
13
+ }
10
14
  /** Puts the text on the clipboard; answers whether it got there. */
11
15
  function copyText(text) {
12
16
  const clipboard = typeof navigator === 'undefined' ? undefined : navigator.clipboard;
@@ -19,7 +23,7 @@ function copyText(text) {
19
23
  * closed by Escape, its button or a click outside it, the focus then back on the button that
20
24
  * opened it.
21
25
  */
22
- function SourceDialog({ text, onClose, onClosed }) {
26
+ function SourceDialog({ text, display, onClose, onClosed }) {
23
27
  const host = useHost();
24
28
  const words = host.strings.editor;
25
29
  const dialog = useRef(null);
@@ -42,17 +46,20 @@ function SourceDialog({ text, onClose, onClosed }) {
42
46
  closed.current();
43
47
  };
44
48
  }, []);
45
- return (_jsx("dialog", { ref: dialog, className: "edit-source", "aria-modal": "true", "aria-labelledby": "edit-source-title", onCancel: (event) => {
49
+ return (_jsx("dialog", { ref: dialog, className: `edit-source${display === 'responsive' ? ' has-responsive-buttons' : ''}`, "aria-modal": "true", "aria-labelledby": "edit-source-title", onCancel: (event) => {
46
50
  event.preventDefault();
47
51
  onClose();
48
52
  },
49
53
  // The dialog has no padding of its own, so a click on it and not on its content is outside.
50
- onClick: (event) => event.target === dialog.current && onClose(), children: _jsxs("div", { className: "edit-source-frame", children: [_jsxs("header", { className: "edit-source-header", children: [_jsx("h2", { id: "edit-source-title", className: "edit-source-title", children: words.sourceTitle }), _jsx("span", { className: "edit-source-copied", role: "status", children: copied === 'copied' ? words.copied : copied === 'failed' ? words.copyFailed : '' }), _jsx("button", { type: "button", className: "edit-control edit-control-labelled", onClick: () => void copyText(text).then((done) => setCopied(done ? 'copied' : 'failed')), children: words.copy }), _jsx("button", { type: "button", className: "edit-control edit-source-close", "aria-label": host.strings.close, title: host.strings.close, onClick: onClose, children: ICON_CLOSE })] }), _jsx("pre", { className: "edit-source-text", tabIndex: 0, children: text })] }) }));
54
+ onClick: (event) => event.target === dialog.current && onClose(), children: _jsxs("div", { className: "edit-source-frame", children: [_jsxs("header", { className: "edit-source-header", children: [_jsx("h2", { id: "edit-source-title", className: "edit-source-title", children: words.sourceTitle }), _jsx("span", { className: "edit-source-copied", role: "status", children: copied === 'copied' ? words.copied : copied === 'failed' ? words.copyFailed : '' }), _jsx("button", { type: "button", className: `edit-control edit-source-copy${display === 'label' ? ' edit-control-labelled' : ''}`, "data-buttons": display, title: words.copy, "aria-label": display === 'label' ? undefined : words.copy, onClick: () => void copyText(text).then((done) => setCopied(done ? 'copied' : 'failed')), children: _jsx(ButtonFace, { icon: copied === 'copied' ? ICON_CHECK : ICON_COPY, word: words.copy, display: display }) }), _jsx("button", { type: "button", className: "edit-control edit-source-close", "aria-label": host.strings.close, title: host.strings.close, onClick: onClose, children: ICON_CLOSE })] }), _jsx("pre", { className: "edit-source-text", tabIndex: 0, children: text })] }) }));
51
55
  }
52
56
  /** How long Copy Markdown shows its check, in milliseconds. */
53
57
  const COPIED_MS = 1600;
54
- /** Show Markdown and Copy Markdown; `text` answers the bytes when a button is used. */
55
- export function NoteActions({ text }) {
58
+ /**
59
+ * Show Markdown and Copy Markdown; `text` answers the bytes when a button is used, and `display`
60
+ * how they and the dialog's Copy show their word.
61
+ */
62
+ export function NoteActions({ text, display = 'icon' }) {
56
63
  const words = useHost().strings.editor;
57
64
  const show = useRef(null);
58
65
  const [source, setSource] = useState(null);
@@ -64,5 +71,5 @@ export function NoteActions({ text }) {
64
71
  return () => clearTimeout(timer);
65
72
  }, [copied]);
66
73
  const copyLabel = copied === 'copied' ? words.copied : copied === 'failed' ? words.copyFailed : words.copyMarkdown;
67
- return (_jsxs(_Fragment, { children: [_jsx("button", { ref: show, type: "button", className: "note-action", title: words.showMarkdown, "aria-label": words.showMarkdown, "aria-haspopup": "dialog", onClick: () => setSource(text()), children: ICON_MARKDOWN }), _jsx("button", { type: "button", className: "note-action", "data-copied": copied ?? undefined, title: copyLabel, "aria-label": words.copyMarkdown, onClick: () => void copyText(text()).then((done) => setCopied(done ? 'copied' : 'failed')), children: copied === 'copied' ? ICON_CHECK : ICON_COPY }), _jsx("span", { className: "visually-hidden", role: "status", children: copied === 'copied' ? words.copied : copied === 'failed' ? words.copyFailed : '' }), source !== null && _jsx(SourceDialog, { text: source, onClose: () => setSource(null), onClosed: () => show.current?.focus() })] }));
74
+ return (_jsxs(_Fragment, { children: [_jsx("button", { ref: show, type: "button", className: `note-action note-action-${display}`, "data-buttons": display, title: words.showMarkdown, "aria-label": display === 'label' ? undefined : words.showMarkdown, "aria-haspopup": "dialog", onClick: () => setSource(text()), children: _jsx(ButtonFace, { icon: ICON_MARKDOWN, word: words.showMarkdown, display: display }) }), _jsx("button", { type: "button", className: `note-action note-action-${display}`, "data-buttons": display, "data-copied": copied ?? undefined, title: copyLabel, "aria-label": words.copyMarkdown, onClick: () => void copyText(text()).then((done) => setCopied(done ? 'copied' : 'failed')), children: _jsx(ButtonFace, { icon: copied === 'copied' ? ICON_CHECK : ICON_COPY, word: words.copyMarkdown, display: display }) }), _jsx("span", { className: "visually-hidden", role: "status", children: copied === 'copied' ? words.copied : copied === 'failed' ? words.copyFailed : '' }), source !== null && _jsx(SourceDialog, { text: source, display: display, onClose: () => setSource(null), onClosed: () => show.current?.focus() })] }));
68
75
  }
@@ -1,3 +1,4 @@
1
+ import { type NoteButtons } from './NoteActions.js';
1
2
  import { type NoteHost } from './host.js';
2
3
  export interface NoteBodyProps extends NoteHost {
3
4
  /**
@@ -26,8 +27,19 @@ export interface NoteBodyProps extends NoteHost {
26
27
  */
27
28
  readonly onChange?: (markdown: string) => unknown;
28
29
  /**
29
- * Whether the switch shows its icon alone or its icon and its word. Defaults to `icon`, like Show
30
- * Markdown and Copy Markdown beside it, its word then its name and its title (#92).
30
+ * How every button the note draws around its text shows its word — Show Markdown, Copy Markdown,
31
+ * the switch, and Copy in the dialog of Show Markdown (#96): `icon`, the icon alone; `label`, the
32
+ * icon and the word; `responsive`, the word beside the icon on a note at least
33
+ * `--markdown-button-words-from` wide (560px by default) and hidden on a narrower one. Wherever
34
+ * the word is not drawn, or may be hidden, it is the button's name and its title. Defaults to
35
+ * `icon`, or to `modeButton` when only that is given.
36
+ */
37
+ readonly buttons?: NoteButtons;
38
+ /**
39
+ * The switch alone as its icon or its icon and its word (#92).
40
+ *
41
+ * @deprecated Use `buttons`, which governs the switch and the buttons beside it; this is read
42
+ * only when `buttons` is left out.
31
43
  */
32
44
  readonly modeButton?: 'icon' | 'label';
33
45
  /**
@@ -45,4 +57,4 @@ export type NoteMode = 'view' | 'edit';
45
57
  * It renders inside `.markdown`, so the stylesheet of the package applies, and asks the host for
46
58
  * every piece of notebook data through the props of `NoteHost`.
47
59
  */
48
- export declare function NoteBody({ editable, mode, defaultMode, onModeChange, onChange, modeButton, actions, ...rest }: NoteBodyProps): import("react").JSX.Element;
60
+ export declare function NoteBody({ editable, mode, defaultMode, onModeChange, onChange, buttons, modeButton, actions, ...rest }: NoteBodyProps): import("react").JSX.Element;
package/dist/NoteBody.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { lazy, Suspense, useMemo, useRef, useState } from 'react';
3
3
  import { ICON_EDIT, ICON_VIEW } from './mode-icons.js';
4
- import { NoteActions } from './NoteActions.js';
4
+ import { ButtonFace, NoteActions } from './NoteActions.js';
5
5
  import { drawSlot } from './slots.js';
6
6
  import { HOST_KEYS, HostProvider, resolveHost, useHost } from './host.js';
7
7
  import { planFootnotes } from './footnotes.js';
@@ -26,7 +26,7 @@ function ModeSwitch({ mode, display, toggle }) {
26
26
  const icon = mode === 'view' ? ICON_EDIT : ICON_VIEW;
27
27
  if (slot)
28
28
  return _jsx(_Fragment, { children: drawSlot(slot, { mode, label, icon, display, toggle }) });
29
- return (_jsxs("button", { type: "button", className: `mode-switch mode-switch-${display}`, "data-mode": mode, title: label, "aria-label": display === 'icon' ? label : undefined, onClick: toggle, children: [icon, display === 'label' && _jsx("span", { className: "mode-switch-label", children: label })] }));
29
+ return (_jsx("button", { type: "button", className: `mode-switch mode-switch-${display}`, "data-mode": mode, "data-buttons": display, title: label, "aria-label": display === 'label' ? undefined : label, onClick: toggle, children: _jsx(ButtonFace, { icon: icon, word: label, display: display, wordClass: "mode-switch-label" }) }));
30
30
  }
31
31
  /**
32
32
  * The body of a note, drawn as MemorySmith's reading page draws it.
@@ -34,13 +34,14 @@ function ModeSwitch({ mode, display, toggle }) {
34
34
  * It renders inside `.markdown`, so the stylesheet of the package applies, and asks the host for
35
35
  * every piece of notebook data through the props of `NoteHost`.
36
36
  */
37
- export function NoteBody({ editable = false, mode, defaultMode = 'view', onModeChange, onChange, modeButton = 'icon', actions = true, ...rest }) {
37
+ export function NoteBody({ editable = false, mode, defaultMode = 'view', onModeChange, onChange, buttons, modeButton = 'icon', actions = true, ...rest }) {
38
38
  const [own, setOwn] = useState(defaultMode);
39
39
  // The author asked to go back to reading, and the editor is finishing: a save, the toolbar folding.
40
40
  const [leaving, setLeaving] = useState(false);
41
41
  // The last text handed to the host in edit mode, which Show and Copy Markdown give there (#76).
42
42
  const handed = useRef(null);
43
43
  const current = editable ? (mode ?? own) : 'view';
44
+ const display = buttons ?? modeButton;
44
45
  if (!editable && !actions)
45
46
  return _jsx(NoteView, { ...rest });
46
47
  const go = (next) => {
@@ -69,12 +70,12 @@ export function NoteBody({ editable = false, mode, defaultMode = 'view', onModeC
69
70
  return (_jsx(HostOnly, { host: rest, children: (resolved) => {
70
71
  // Show and Copy Markdown for whoever reads the note, in both modes (#76, #87), and the
71
72
  // switch only where the note is editable.
72
- const corner = (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: "note-corner", children: [actions && _jsx(NoteActions, { text: () => (current === 'edit' ? (handed.current ?? rest.markdown) : rest.markdown) }), editable && _jsx(ModeSwitch, { mode: current, display: modeButton, toggle: toggle })] }) }));
73
+ const corner = (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: "note-corner", children: [actions && _jsx(NoteActions, { display: display, text: () => (current === 'edit' ? (handed.current ?? rest.markdown) : rest.markdown) }), editable && _jsx(ModeSwitch, { mode: current, display: display, toggle: toggle })] }) }));
73
74
  if (current === 'view')
74
- return _jsx(NoteView, { ...rest, corner: corner, switched: editable });
75
+ return _jsx(NoteView, { ...rest, corner: corner, switched: editable, buttons: display });
75
76
  return (
76
77
  // While the editor loads, the note stays on the page as it was.
77
- _jsx(Suspense, { fallback: _jsx(NoteView, { ...rest, corner: corner, switched: true }), children: _jsx(NoteEditor, { ...host, onChange: hand, corner: corner, autoFocus: true, leaving: leaving, onLeave: onLeave }) }));
78
+ _jsx(Suspense, { fallback: _jsx(NoteView, { ...rest, corner: corner, switched: true, buttons: display }), children: _jsx(NoteEditor, { ...host, onChange: hand, corner: corner, buttons: display, autoFocus: true, leaving: leaving, onLeave: onLeave }) }));
78
79
  } }));
79
80
  }
80
81
  /** The host resolved once, for what is drawn around the note. */
@@ -83,7 +84,7 @@ function HostOnly({ host, children }) {
83
84
  return _jsx(_Fragment, { children: children(resolved) });
84
85
  }
85
86
  /** The note drawn for reading: the page MemorySmith's reading page draws. */
86
- function NoteView({ markdown, writable = false, onToggleTask, corner, switched = false, ...host }) {
87
+ function NoteView({ markdown, writable = false, onToggleTask, corner, switched = false, buttons = 'icon', ...host }) {
87
88
  const resolved = useMemo(() => resolveHost(host),
88
89
  // Every member of the contract, so a host that passes the same functions keeps the same host.
89
90
  HOST_KEYS.map((key) => host[key]));
@@ -147,7 +148,7 @@ function NoteView({ markdown, writable = false, onToggleTask, corner, switched =
147
148
  // The runs and whether an embed cut them are the whole input.
148
149
  [runs.join('\u0000'), runs.length < segments.length]);
149
150
  let run = 0;
150
- return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name}${corner ? ' has-note-corner' : ''}${switched ? ' has-mode-switch' : ''}`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme), children: [corner, _jsx(Properties, { properties: properties }), segments.map((segment, index) => {
151
+ return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name}${corner ? ' has-note-corner' : ''}${switched ? ' has-mode-switch' : ''}${corner && buttons === 'responsive' ? ' has-responsive-buttons' : ''}`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme), children: [corner, _jsx(Properties, { properties: properties }), segments.map((segment, index) => {
151
152
  if (!('rendered' in segment)) {
152
153
  return _jsx(Transclusion, { target: segment.target, anchor: segment.anchor }, index);
153
154
  }
@@ -9,6 +9,7 @@
9
9
  */
10
10
  import { type ReactNode } from 'react';
11
11
  import { type NoteHost } from '../host.js';
12
+ import { type NoteButtons } from '../NoteActions.js';
12
13
  export interface NoteEditorProps extends Omit<NoteHost, 'writable' | 'onToggleTask'> {
13
14
  /** The note as the host keeps it, frontmatter included. A new value that differs from the editor's is taken in. */
14
15
  readonly markdown: string;
@@ -22,6 +23,11 @@ export interface NoteEditorProps extends Omit<NoteHost, 'writable' | 'onToggleTa
22
23
  readonly editable?: boolean;
23
24
  /** What sits at the top right of the note, such as the switch back to reading. */
24
25
  readonly corner?: ReactNode;
26
+ /**
27
+ * How the buttons at the top right show their word, as `NoteBody`'s `buttons` (#96): the bar is
28
+ * what a `responsive` button measures. Defaults to `icon`.
29
+ */
30
+ readonly buttons?: NoteButtons;
25
31
  /** Whether the editor takes the focus when it opens. */
26
32
  readonly autoFocus?: boolean;
27
33
  /**
@@ -41,4 +47,4 @@ export declare function lineBreakOf(text: string): '\n' | '\r\n' | null;
41
47
  * Edits a note on the page that draws it. Exported at `@memorysmithapp/markdown-renderer/edit`
42
48
  * for a host that edits without the switch of `NoteBody`; `NoteBody` loads it on demand.
43
49
  */
44
- export declare function NoteEditor({ markdown: given, onChange, editable, corner, autoFocus, leaving, onLeave, ...host }: NoteEditorProps): import("react").JSX.Element;
50
+ export declare function NoteEditor({ markdown: given, onChange, editable, corner, buttons, autoFocus, leaving, onLeave, ...host }: NoteEditorProps): import("react").JSX.Element;
@@ -171,7 +171,7 @@ function SaveStatus({ saves, blocked, host, onDecided }) {
171
171
  * Edits a note on the page that draws it. Exported at `@memorysmithapp/markdown-renderer/edit`
172
172
  * for a host that edits without the switch of `NoteBody`; `NoteBody` loads it on demand.
173
173
  */
174
- export function NoteEditor({ markdown: given, onChange, editable = true, corner, autoFocus = false, leaving = false, onLeave, ...host }) {
174
+ export function NoteEditor({ markdown: given, onChange, editable = true, corner, buttons = 'icon', autoFocus = false, leaving = false, onLeave, ...host }) {
175
175
  const resolved = useMemo(() => resolveHost(host),
176
176
  // Every member of the contract, so a host that passes the same functions keeps the same host.
177
177
  HOST_KEYS.map((key) => host[key]));
@@ -504,7 +504,7 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
504
504
  clearTimeout(timer);
505
505
  };
506
506
  }, [leaving, saves.state]);
507
- return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name} markdown-editing`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme), children: [_jsx("div", { ref: bar, className: "edit-bar", "data-leaving": folding ? '' : undefined, children: _jsx(Toolbar, { view: view, host: resolved, opened: opened.current, onAttach: () => {
507
+ return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name} markdown-editing`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme), children: [_jsx("div", { ref: bar, className: `edit-bar${buttons === 'responsive' ? ' has-responsive-buttons' : ''}`, "data-leaving": folding ? '' : undefined, children: _jsx(Toolbar, { view: view, host: resolved, opened: opened.current, onAttach: () => {
508
508
  // Opening the paperclip is the author acting on a refusal: the dialog says it once more.
509
509
  if (attaching.state === 'failed') {
510
510
  setRefusal(attaching.message);
@@ -513,7 +513,7 @@ export function NoteEditor({ markdown: given, onChange, editable = true, corner,
513
513
  else
514
514
  setRefusal(null);
515
515
  setAttachOpen(true);
516
- }, attaching: attaching, attachButton: attachButton, end: _jsxs("div", { className: "edit-corner", "aria-busy": leaving && saves.state === 'saving' ? true : undefined, "data-blocked": blocked ? '' : undefined, children: [_jsx(SaveStatus, { saves: saves, blocked: blocked, host: resolved, onDecided: () => view?.focus() }), corner ?? _jsx(NoteActions, { text: () => (view ? output(view.state) : given) })] }, blocked ? `blocked-${refusals}` : 'corner') }) }), menu && view && (_jsx(ContextMenu, { x: menu.x, y: menu.y, label: resolved.strings.editor.menu, groups: menuGroups(view), onClose: (back) => {
516
+ }, attaching: attaching, attachButton: attachButton, end: _jsxs("div", { className: "edit-corner", "aria-busy": leaving && saves.state === 'saving' ? true : undefined, "data-blocked": blocked ? '' : undefined, children: [_jsx(SaveStatus, { saves: saves, blocked: blocked, host: resolved, onDecided: () => view?.focus() }), corner ?? _jsx(NoteActions, { display: buttons, text: () => (view ? output(view.state) : given) })] }, blocked ? `blocked-${refusals}` : 'corner') }) }), menu && view && (_jsx(ContextMenu, { x: menu.x, y: menu.y, label: resolved.strings.editor.menu, groups: menuGroups(view), onClose: (back) => {
517
517
  setMenu(null);
518
518
  if (back)
519
519
  view.focus();
@@ -29,6 +29,18 @@ export declare function CalloutBlock({ view, text, marker }: {
29
29
  fold: '+' | '-' | null;
30
30
  };
31
31
  }): import("react").JSX.Element;
32
+ /**
33
+ * An image drawn in edit mode with the size it is drawn at set in place (#98): a width in pixels,
34
+ * its height following, a handle at its corner that drags it, and a button back to its own size.
35
+ * Each writes the dimensions of its notation — `![description|300](…)` or `![[photo.jpg|300]]` —
36
+ * and no other byte. The controls appear once an image is drawn inside it, so an embed of a file
37
+ * that is not an image, or a host's own drawing without one, offers none.
38
+ */
39
+ export declare function ImageBlock({ view, source, children }: {
40
+ view: EditorView;
41
+ source: string;
42
+ children?: ReactNode;
43
+ }): import("react").JSX.Element;
32
44
  /**
33
45
  * A table whose cells are edited where they stand. A cell commits its text when it loses the focus,
34
46
  * or on Enter (to the cell below) and Tab (to the next one); each commit rewrites that cell's
@@ -17,6 +17,8 @@ import { isTrue, readProperties } from '../properties.js';
17
17
  import { Transclusion } from '../Transclusion.js';
18
18
  import { splitEmbeds } from '../transclusion.js';
19
19
  import { wikilinkAddress } from '../wikilink-address.js';
20
+ import { ICON_OWN_SIZE } from '../mode-icons.js';
21
+ import { MAX_IMAGE_WIDTH, readImageSize, resizeImage } from './image-size.js';
20
22
  import { addEntry, readFrontmatter, removeEntry, renameKey, setList, setScalar, } from './frontmatter.js';
21
23
  import { addColumn, addRow, alignColumn, columnCount, readTable, removeColumn, removeRow, setCell, } from './table.js';
22
24
  /** Whether a block's edit is being applied: the block is drawn anew, and the field it held lets the focus go. */
@@ -71,6 +73,110 @@ export function CalloutBlock({ view, text, marker }) {
71
73
  const rewrite = (type, fold) => write(view, [{ from: marker.from, to: marker.to, insert: `[!${type}]${fold ?? ''}` }]);
72
74
  return (_jsxs("div", { className: "edit-block edit-callout", children: [_jsxs("div", { className: "edit-block-controls", "data-ms-control": "", children: [_jsx("select", { "aria-label": words.calloutType, title: words.calloutType, value: marker.type, onChange: (event) => rewrite(event.target.value, marker.fold), children: types.map((type) => (_jsx("option", { value: type, children: strings.callouts[type] ?? type }, type))) }), _jsxs("select", { "aria-label": words.fold, title: words.fold, value: marker.fold ?? '', onChange: (event) => rewrite(marker.type, (event.target.value || null)), children: [_jsx("option", { value: "", children: words.foldNone }), _jsx("option", { value: "+", children: words.foldOpen }), _jsx("option", { value: "-", children: words.foldClosed })] })] }), _jsx(Snippet, { text: text })] }));
73
75
  }
76
+ // ── Images ───────────────────────────────────────────────────────────────────
77
+ /** The narrowest an image is dragged to, so its handle stays within reach. */
78
+ const MIN_DRAGGED_WIDTH = 24;
79
+ /**
80
+ * An image drawn in edit mode with the size it is drawn at set in place (#98): a width in pixels,
81
+ * its height following, a handle at its corner that drags it, and a button back to its own size.
82
+ * Each writes the dimensions of its notation — `![description|300](…)` or `![[photo.jpg|300]]` —
83
+ * and no other byte. The controls appear once an image is drawn inside it, so an embed of a file
84
+ * that is not an image, or a host's own drawing without one, offers none.
85
+ */
86
+ export function ImageBlock({ view, source, children }) {
87
+ const words = useHost().strings.editor;
88
+ const frame = useRef(null);
89
+ const size = readImageSize(source);
90
+ const [image, setImage] = useState(null);
91
+ const [natural, setNatural] = useState(null);
92
+ const [typed, setTyped] = useState(null);
93
+ const [dragged, setDragged] = useState(null);
94
+ const drag = useRef(null);
95
+ // An embed's image arrives when the host answers; the controls wait for it.
96
+ useEffect(() => {
97
+ const element = frame.current;
98
+ if (!element)
99
+ return;
100
+ const look = () => setImage(element.querySelector('img'));
101
+ look();
102
+ if (typeof MutationObserver === 'undefined')
103
+ return;
104
+ const observer = new MutationObserver(look);
105
+ observer.observe(element, { childList: true, subtree: true });
106
+ return () => observer.disconnect();
107
+ }, []);
108
+ useEffect(() => {
109
+ if (!image)
110
+ return;
111
+ const read = () => setNatural(image.naturalWidth || null);
112
+ read();
113
+ image.addEventListener('load', read);
114
+ return () => image.removeEventListener('load', read);
115
+ }, [image]);
116
+ const resize = (width) => {
117
+ const widget = frame.current?.closest('.cm-ms-inline-block, .cm-ms-block');
118
+ if (!widget)
119
+ return;
120
+ const from = view.posAtDOM(widget);
121
+ // The widget stands where its source was: anything else means the note moved under it.
122
+ if (view.state.sliceDoc(from, from + source.length) !== source)
123
+ return;
124
+ write(view, resizeImage(source, from, width));
125
+ };
126
+ const commitTyped = () => {
127
+ if (typed === null)
128
+ return;
129
+ const width = Number(typed.trim());
130
+ setTyped(null);
131
+ if (typed.trim() === '')
132
+ resize(null);
133
+ else if (Number.isFinite(width) && width >= 1)
134
+ resize(width);
135
+ };
136
+ const onKeyDown = (event) => {
137
+ if (event.key === 'Enter') {
138
+ event.preventDefault();
139
+ commitTyped();
140
+ }
141
+ else if (event.key === 'Escape') {
142
+ event.preventDefault();
143
+ setTyped(null);
144
+ }
145
+ };
146
+ const shown = dragged ?? size?.width ?? null;
147
+ const offered = size !== null && image !== null;
148
+ return (_jsxs("span", { ref: frame, className: "edit-image", "data-resizing": dragged === null ? undefined : '', children: [children, offered && (_jsxs(_Fragment, { children: [_jsxs("span", { className: "edit-block-controls edit-image-controls", "data-ms-control": "", children: [_jsx("input", { className: "edit-field edit-image-width", type: "number", inputMode: "numeric", min: 1, max: MAX_IMAGE_WIDTH, "aria-label": words.imageWidth, title: words.imageWidth, placeholder: natural === null ? '' : String(natural), value: typed ?? (shown === null ? '' : String(shown)), onChange: (event) => setTyped(event.target.value), onKeyDown: onKeyDown, onBlur: commitTyped }), _jsx("span", { className: "edit-image-unit", "aria-hidden": "true", children: "px" }), _jsx(Control, { label: words.imageOwnSize, onClick: () => resize(null), pressed: size.width === null, children: ICON_OWN_SIZE })] }), _jsx("span", { className: "edit-image-handle", "data-ms-control": "", title: words.imageResize, "aria-hidden": "true", onPointerDown: (event) => {
149
+ event.preventDefault();
150
+ event.currentTarget.setPointerCapture?.(event.pointerId);
151
+ drag.current = { x: event.clientX, width: image.getBoundingClientRect().width, pointer: event.pointerId };
152
+ setDragged(Math.round(drag.current.width));
153
+ }, onPointerMove: (event) => {
154
+ const start = drag.current;
155
+ if (!start || start.pointer !== event.pointerId)
156
+ return;
157
+ const width = Math.max(MIN_DRAGGED_WIDTH, Math.round(start.width + event.clientX - start.x));
158
+ image.style.width = `${width}px`;
159
+ image.style.height = 'auto';
160
+ // What the page lets it be: never wider than the column.
161
+ setDragged(Math.round(image.getBoundingClientRect().width));
162
+ }, onPointerUp: (event) => {
163
+ const start = drag.current;
164
+ if (!start || start.pointer !== event.pointerId)
165
+ return;
166
+ drag.current = null;
167
+ const width = Math.round(image.getBoundingClientRect().width);
168
+ setDragged(null);
169
+ image.style.width = '';
170
+ image.style.height = '';
171
+ if (Math.abs(width - start.width) >= 1)
172
+ resize(width);
173
+ }, onPointerCancel: () => {
174
+ drag.current = null;
175
+ setDragged(null);
176
+ image.style.width = '';
177
+ image.style.height = '';
178
+ } })] }))] }));
179
+ }
74
180
  // ── Tables ───────────────────────────────────────────────────────────────────
75
181
  /** Where the focus goes back to when a table is drawn again after an edit of one of its cells. */
76
182
  let pendingFocus = null;
@@ -13,6 +13,22 @@ export interface BlockMetrics {
13
13
  readonly width: number;
14
14
  readonly lineHeight: number;
15
15
  readonly charWidth: number;
16
+ /**
17
+ * Each level of heading as the theme draws it in edit mode, `h1` first, and a line of a code
18
+ * block: its line height, its characters, the space above it and the width its sides take (#94).
19
+ * Absent where nothing is laid out.
20
+ */
21
+ readonly headings?: readonly LineMetrics[];
22
+ readonly code?: LineMetrics;
23
+ }
24
+ /** A kind of line as the theme draws it, which differs from the text's: a heading, a line of code. */
25
+ export interface LineMetrics {
26
+ readonly lineHeight: number;
27
+ readonly charWidth: number;
28
+ /** The space above it, on a heading. */
29
+ readonly padding: number;
30
+ /** The width its sides take from the column: a code block's padding and border. */
31
+ readonly inset: number;
16
32
  }
17
33
  /** What an estimate assumes before the editor has been measured, as in a test with no layout. */
18
34
  export declare const DEFAULT_METRICS: BlockMetrics;
@@ -22,10 +38,28 @@ export declare const DEFAULT_METRICS: BlockMetrics;
22
38
  * nothing is laid out.
23
39
  */
24
40
  export declare function measureMetrics(parent: HTMLElement): BlockMetrics;
41
+ /**
42
+ * Each level of heading and a line of code laid out in `parent` as the editor draws them, by the
43
+ * classes of their line, so whatever the theme gives them — a face, a size, the space above, the
44
+ * sides of a code block — is what is measured (#94).
45
+ */
46
+ export declare function measureLines(parent: HTMLElement): {
47
+ headings: LineMetrics[];
48
+ code: LineMetrics;
49
+ };
25
50
  /** Metrics as measured, any that could not be read replaced by the default. */
26
51
  export declare function withinReason(metrics: BlockMetrics): BlockMetrics;
52
+ /**
53
+ * The height of a line of the text the editor shows, `length` characters visible, as the page draws
54
+ * it where CodeMirror would guess it from the text's metrics alone (#94): a heading at its `level`,
55
+ * a line of code, or a line of text, which on a narrow column wraps sooner than CodeMirror counts.
56
+ * `null` where that kind of line was not measured.
57
+ */
58
+ export declare function lineHeightOf(kind: 'text' | 'code' | {
59
+ readonly heading: number;
60
+ }, length: number, metrics: BlockMetrics): number | null;
27
61
  /** What a block is, for its estimate. */
28
- export type BlockKind = 'frontmatter' | 'callout' | 'table' | 'embed' | 'diagram' | 'preview' | 'add-properties';
62
+ export type BlockKind = 'frontmatter' | 'callout' | 'table' | 'embed' | 'diagram' | 'preview' | 'add-properties' | 'image';
29
63
  /** The height a block of this kind and these lines is drawn at, near enough to lay out the page. */
30
64
  export declare function estimateHeight(kind: BlockKind, source: string, metrics: BlockMetrics): number;
31
65
  /** The height a block of this content was drawn at at this width, if it was. */
@@ -8,6 +8,7 @@
8
8
  * properties — and a block drawn once at a width is remembered at its measured height, so the same
9
9
  * block made again, moved by an edit above it or in the next edit of the note, starts right.
10
10
  */
11
+ import { readImageSize } from './image-size.js';
11
12
  /** What an estimate assumes before the editor has been measured, as in a test with no layout. */
12
13
  export const DEFAULT_METRICS = { width: 700, lineHeight: 24, charWidth: 8 };
13
14
  /**
@@ -17,23 +18,74 @@ export const DEFAULT_METRICS = { width: 700, lineHeight: 24, charWidth: 8 };
17
18
  */
18
19
  export function measureMetrics(parent) {
19
20
  const probe = document.createElement('div');
20
- probe.style.cssText = 'position: absolute; visibility: hidden; white-space: pre; pointer-events: none';
21
- probe.textContent = 'abcdefghijklmnopqrstuvwxyz';
21
+ probe.style.cssText = PROBE;
22
+ probe.textContent = SAMPLE;
22
23
  parent.appendChild(probe);
23
24
  const box = probe.getBoundingClientRect();
24
25
  const width = parent.clientWidth;
25
26
  probe.remove();
26
- return withinReason({ width, lineHeight: box.height, charWidth: box.width / 26 });
27
+ return withinReason({ width, lineHeight: box.height, charWidth: box.width / SAMPLE.length, ...measureLines(parent) });
28
+ }
29
+ const PROBE = 'position: absolute; visibility: hidden; white-space: pre; pointer-events: none';
30
+ const SAMPLE = 'abcdefghijklmnopqrstuvwxyz';
31
+ /**
32
+ * Each level of heading and a line of code laid out in `parent` as the editor draws them, by the
33
+ * classes of their line, so whatever the theme gives them — a face, a size, the space above, the
34
+ * sides of a code block — is what is measured (#94).
35
+ */
36
+ export function measureLines(parent) {
37
+ const classes = [1, 2, 3, 4, 5, 6].map((level) => `cm-ms-heading cm-ms-h${level}`).concat('cm-ms-code-line');
38
+ const probes = classes.map((className) => {
39
+ const probe = document.createElement('div');
40
+ probe.className = `cm-line ${className}`;
41
+ probe.style.cssText = PROBE;
42
+ probe.textContent = SAMPLE;
43
+ parent.appendChild(probe);
44
+ return probe;
45
+ });
46
+ const read = probes.map((probe) => {
47
+ const box = probe.getBoundingClientRect();
48
+ const style = getComputedStyle(probe);
49
+ const number = (value) => Number.parseFloat(value) || 0;
50
+ const padding = number(style.paddingTop);
51
+ const inset = number(style.paddingLeft) + number(style.paddingRight) + number(style.borderLeftWidth) + number(style.borderRightWidth);
52
+ return { lineHeight: box.height - padding - number(style.paddingBottom), charWidth: (box.width - inset) / SAMPLE.length, padding, inset };
53
+ });
54
+ for (const probe of probes)
55
+ probe.remove();
56
+ return { headings: read.slice(0, 6), code: read[6] };
27
57
  }
28
58
  /** Metrics as measured, any that could not be read replaced by the default. */
29
59
  export function withinReason(metrics) {
30
60
  const usable = (value, fallback) => (Number.isFinite(value) && value > 1 ? value : fallback);
61
+ const laidOut = (line) => line.lineHeight > 1 && line.charWidth > 0.5;
62
+ const headings = metrics.headings?.length === 6 && metrics.headings.every(laidOut) ? metrics.headings : undefined;
63
+ const code = metrics.code && laidOut(metrics.code) ? metrics.code : undefined;
31
64
  return {
32
65
  width: usable(metrics.width, DEFAULT_METRICS.width),
33
66
  lineHeight: usable(metrics.lineHeight, DEFAULT_METRICS.lineHeight),
34
67
  charWidth: usable(metrics.charWidth, DEFAULT_METRICS.charWidth),
68
+ ...(headings ? { headings } : {}),
69
+ ...(code ? { code } : {}),
35
70
  };
36
71
  }
72
+ /** A line of `length` visible characters drawn as `line`, wrapped at the column's width. */
73
+ function lineOf(line, length, width) {
74
+ const perLine = Math.max(4, Math.floor((width - line.inset) / line.charWidth));
75
+ return Math.round(line.padding + line.lineHeight * Math.max(1, Math.ceil(length / perLine)));
76
+ }
77
+ /**
78
+ * The height of a line of the text the editor shows, `length` characters visible, as the page draws
79
+ * it where CodeMirror would guess it from the text's metrics alone (#94): a heading at its `level`,
80
+ * a line of code, or a line of text, which on a narrow column wraps sooner than CodeMirror counts.
81
+ * `null` where that kind of line was not measured.
82
+ */
83
+ export function lineHeightOf(kind, length, metrics) {
84
+ if (kind === 'text')
85
+ return lineOf({ lineHeight: metrics.lineHeight, charWidth: metrics.charWidth, padding: 0, inset: 0 }, length, metrics.width);
86
+ const line = kind === 'code' ? metrics.code : metrics.headings?.[kind.heading - 1];
87
+ return line ? lineOf(line, length, metrics.width) : null;
88
+ }
37
89
  /** The lines a text takes in a column `width` wide, each of its lines at least one. */
38
90
  function wrapped(lines, width, metrics) {
39
91
  const perLine = Math.max(8, Math.floor(width / metrics.charWidth));
@@ -44,6 +96,8 @@ function wrapped(lines, width, metrics) {
44
96
  * the text column of its 560px breakpoint.
45
97
  */
46
98
  const STACKED_PROPERTIES = 528;
99
+ /** What an image declares no height for is drawn at before it loads, near enough (#91). */
100
+ const UNDECLARED_IMAGE_HEIGHT = 200;
47
101
  /** The height a block of this kind and these lines is drawn at, near enough to lay out the page. */
48
102
  export function estimateHeight(kind, source, metrics) {
49
103
  const { lineHeight } = metrics;
@@ -79,6 +133,13 @@ export function estimateHeight(kind, source, metrics) {
79
133
  return Math.round(lineHeight + wrapped(lines, metrics.width, metrics) * lineHeight);
80
134
  case 'add-properties':
81
135
  return Math.round(1.5 * lineHeight);
136
+ case 'image': {
137
+ // The height it declares, or its width at a photograph's proportions, or a guess (#91).
138
+ const size = readImageSize(source.trim());
139
+ if (size?.height != null)
140
+ return size.height;
141
+ return size?.width != null ? Math.round(size.width * 0.75) : UNDECLARED_IMAGE_HEIGHT;
142
+ }
82
143
  }
83
144
  }
84
145
  /** The heights blocks were drawn at, by width and content; the oldest are let go past the limit. */
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The size an image is drawn at, as its notation writes it (#98): the dimensions after the pipe
3
+ * of a Markdown image's description, `![description|300](address)` (`SPEC.md` §3.14), or of the
4
+ * embed of a file the notebook keeps, `![[photo.jpg|300]]` (§5.7). The editor reads them and
5
+ * rewrites them, and no other byte.
6
+ */
7
+ import type { TextChange } from './table.js';
8
+ export interface ImageSize {
9
+ /** The width it declares, or `null` when it is drawn at its own size. */
10
+ readonly width: number | null;
11
+ readonly height: number | null;
12
+ /**
13
+ * Where in the source the dimensions are written, their pipe included; an empty span where they
14
+ * would be written when there are none.
15
+ */
16
+ readonly at: {
17
+ readonly from: number;
18
+ readonly to: number;
19
+ };
20
+ /** What separates them from the rest: `|`, or `\|` in an embed whose pipe is escaped. */
21
+ readonly pipe: string;
22
+ }
23
+ /** The largest width the notation writes: five digits (`SPEC.md` §3.14). */
24
+ export declare const MAX_IMAGE_WIDTH = 99999;
25
+ /**
26
+ * The size an image's source declares, and where it is written. `null` for a source that is not an
27
+ * image's notation, and for an embed whose pipe holds something other than dimensions, which the
28
+ * editor does not overwrite.
29
+ */
30
+ export declare function readImageSize(source: string): ImageSize | null;
31
+ /**
32
+ * The change that draws the image whose source starts at `from` at `width`, its height following,
33
+ * or at its own size for `null`: the dimensions written, replaced or taken away, and nothing else.
34
+ * Empty when the source is not an image's notation or already says so.
35
+ */
36
+ export declare function resizeImage(source: string, from: number, width: number | null): TextChange[];