@astl/editor-tiptap 0.3.3 → 0.4.1

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/src/nodes.ts CHANGED
@@ -102,6 +102,7 @@ import {
102
102
  type SurfaceChange,
103
103
  } from './blocks.js';
104
104
  import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
105
+ import { PAGE_ACTIONS, type PageAction, pageActionsOf } from './page-actions.js';
105
106
  import { inert } from './source.js';
106
107
  import { setPlacedStyle } from './style.js';
107
108
 
@@ -2111,6 +2112,25 @@ const layoutNodes: readonly Node[] = [
2111
2112
  /** Page heads made so far: each gets ids of its own for its title field's name. */
2112
2113
  let pageHeads = 0;
2113
2114
 
2115
+ /** What an editor's page heads draw (`pageActions`), and how each head draws itself again when that changes. */
2116
+ interface PageHeads {
2117
+ actions: readonly PageAction[];
2118
+ readonly redraws: Set<() => void>;
2119
+ }
2120
+
2121
+ /** The page heads of an editor, in its storage under the page node's name. */
2122
+ const pageHeadsOf = (editor: Editor): PageHeads =>
2123
+ (editor.storage as unknown as Readonly<Record<string, PageHeads>>)[PAGE] as PageHeads;
2124
+
2125
+ /** Draws the page heads of `editor` with these buttons from now on (`<astl-editor>`'s `pageActions`). */
2126
+ export function setPageActions(editor: Editor, actions: readonly string[]): void {
2127
+ const heads = pageHeadsOf(editor);
2128
+ const next = pageActionsOf(actions);
2129
+ if (next.join(' ') === heads.actions.join(' ')) return;
2130
+ heads.actions = next;
2131
+ for (const redraw of heads.redraws) redraw();
2132
+ }
2133
+
2114
2134
  /**
2115
2135
  * `<astl-page>` with a head: the page's number of all (`surface.pageNumber`), "Add title" (`surface.pageAddTitle`: a
2116
2136
  * page has no title until it gets one), from the second page on a button that joins it to the page before
@@ -2118,10 +2138,11 @@ let pageHeads = 0;
2118
2138
  * the head in its field, named by the page's number (`surface.pageTitleOf`, placeholder `surface.pageTitle`), with
2119
2139
  * "Remove title". The field opens while the page has a label and while it has the focus, and closes when it loses the
2120
2140
  * focus empty, without touching the document. The buttons are named by their text and the number, the join button by
2121
- * the page before, so each control has a name of its own. A node view never learns its index: a plugin numbers the
2122
- * heads after every change of the document.
2141
+ * the page before, so each control has a name of its own, and each has `data-action` (`add-title`, `check`, `join`,
2142
+ * `delete`, `remove-title`). `actions` are the buttons drawn, in order: "Remove title" goes with "Add title". A node
2143
+ * view never learns its index: a plugin numbers the heads after every change of the document.
2123
2144
  */
2124
- export function pageNode(authoring: Authoring): Node {
2145
+ export function pageNode(authoring: Authoring, actions: readonly string[] = PAGE_ACTIONS): Node {
2125
2146
  const t = (key: string, params?: MessageParams) => authoring.t(EDITOR_NAMESPACE, key, params);
2126
2147
  // UI texts in the content: in the UI language.
2127
2148
  const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
@@ -2153,9 +2174,11 @@ export function pageNode(authoring: Authoring): Node {
2153
2174
  parseHTML: () => [{ tag: 'astl-page' }],
2154
2175
  renderHTML: ({ node }) => ['astl-page', { ...node.attrs.attributes }, 0],
2155
2176
  addProseMirrorPlugins: () => [numbering],
2177
+ addStorage: (): PageHeads => ({ actions: pageActionsOf(actions), redraws: new Set() }),
2156
2178
  addNodeView:
2157
2179
  () =>
2158
2180
  ({ node, getPos, editor }) => {
2181
+ const drawn = pageHeadsOf(editor);
2159
2182
  const dom = document.createElement('section');
2160
2183
  dom.className = 'astl-page';
2161
2184
  const head = document.createElement('div');
@@ -2177,6 +2200,7 @@ export function pageNode(authoring: Authoring): Node {
2177
2200
  ...ui,
2178
2201
  });
2179
2202
  button.setAttribute('aria-labelledby', `${button.id} ${number.id}`);
2203
+ button.dataset.action = suffix;
2180
2204
  return button;
2181
2205
  };
2182
2206
  const addTitle = named('add-title', t('surface.pageAddTitle'));
@@ -2190,6 +2214,7 @@ export function pageNode(authoring: Authoring): Node {
2190
2214
  hidden: true,
2191
2215
  ...ui,
2192
2216
  });
2217
+ join.dataset.action = 'join';
2193
2218
  const remove = named('delete', t('surface.pageDelete'));
2194
2219
  remove.hidden = true;
2195
2220
  const field = Object.assign(document.createElement('div'), {
@@ -2201,7 +2226,24 @@ export function pageNode(authoring: Authoring): Node {
2201
2226
  placeholder: t('surface.pageTitle'),
2202
2227
  });
2203
2228
  const removeTitle = named('remove-title', t('surface.pageRemoveTitle'));
2204
- field.append(input, removeTitle);
2229
+ const buttons: Readonly<Record<PageAction, HTMLButtonElement>> = {
2230
+ 'add-title': addTitle,
2231
+ check,
2232
+ join,
2233
+ delete: remove,
2234
+ };
2235
+ field.append(input);
2236
+ head.append(number, field);
2237
+ // What is not listed is not drawn at all; its adapter's method stays for the host's own controls. The number and
2238
+ // the field stay put, so a title being typed keeps the focus.
2239
+ const draw = () => {
2240
+ const shown = drawn.actions.map((action) => buttons[action]);
2241
+ for (const button of Object.values(buttons)) if (!shown.includes(button)) button.remove();
2242
+ field.before(...shown);
2243
+ if (drawn.actions.includes('add-title')) field.append(removeTitle);
2244
+ else removeTitle.remove();
2245
+ };
2246
+ drawn.redraws.add(draw);
2205
2247
  let attributes = node.attrs.attributes as Attributes;
2206
2248
  // The author is in the field, or just asked for it: it stays while empty, till the focus leaves.
2207
2249
  let editing = false;
@@ -2280,7 +2322,7 @@ export function pageNode(authoring: Authoring): Node {
2280
2322
  // As after a join: the editor takes the focus, the cursor at the start of the page it went to.
2281
2323
  editor.view.focus();
2282
2324
  });
2283
- head.append(number, addTitle, check, join, remove, field);
2325
+ draw();
2284
2326
  const contentDOM = document.createElement('div');
2285
2327
  // The page's look is for its content, not for the controls in its head.
2286
2328
  const paint = painter(contentDOM, 'astl-page');
@@ -2311,6 +2353,7 @@ export function pageNode(authoring: Authoring): Node {
2311
2353
  // events and mutations alone.
2312
2354
  stopEvent: (event) => head.contains(event.target as HTMLElement),
2313
2355
  ignoreMutation: (m) => head.contains(m.target),
2356
+ destroy: () => drawn.redraws.delete(draw),
2314
2357
  };
2315
2358
  },
2316
2359
  });
@@ -3384,10 +3427,16 @@ function referencesExtension(authoring: Authoring, manifests: readonly Component
3384
3427
  });
3385
3428
  }
3386
3429
 
3430
+ /**
3431
+ * The TipTap extensions of the content format, labelled in `authoring`'s locale: the nodes of `manifests` (default: the
3432
+ * authoring's types), pages with their heads, pictures, layouts and help for students. `pictures` is how the surface
3433
+ * asks about pictures; `pageActions` are the buttons of each page's head, in order (default: `PAGE_ACTIONS`, all four),
3434
+ * read when the extensions are made.
3435
+ */
3387
3436
  export function astrolabeExtensions(
3388
3437
  authoring: Authoring,
3389
3438
  manifests: readonly ComponentManifest[] = authoring.engine.manifests,
3390
- options: { readonly pictures?: PictureHooks } = {},
3439
+ options: { readonly pictures?: PictureHooks; readonly pageActions?: readonly PageAction[] } = {},
3391
3440
  ): Extensions {
3392
3441
  const hooks = options.pictures;
3393
3442
  const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
@@ -3409,7 +3458,7 @@ export function astrolabeExtensions(
3409
3458
  ...formatMarks,
3410
3459
  contentAttributes,
3411
3460
  docNode,
3412
- pageNode(authoring),
3461
+ pageNode(authoring, options.pageActions),
3413
3462
  ...layoutNodes,
3414
3463
  currentLayoutExtension((node) => types.has(node.type.name)),
3415
3464
  rulesExtension,
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The buttons of a page's head on the TipTap surface. A module of its own: the element's entry must not import TipTap,
3
+ * and the `/tiptap` entry must not import the element.
4
+ */
5
+
6
+ /** A button of a page's head on the TipTap surface, for `pageActions`. */
7
+ export type PageAction = 'add-title' | 'check' | 'join' | 'delete';
8
+
9
+ /**
10
+ * Every button of a page's head, in their default order: "Add title" (with "Remove title" beside a page's title),
11
+ * "Check this page", "Join with page N" and "Delete page".
12
+ */
13
+ export const PAGE_ACTIONS: readonly PageAction[] = ['add-title', 'check', 'join', 'delete'];
14
+
15
+ /** A host's list as the heads draw it: known actions, each once, in its order. */
16
+ export function pageActionsOf(list: readonly string[]): readonly PageAction[] {
17
+ return PAGE_ACTIONS.filter((action) => list.includes(action)).sort(
18
+ (a, b) => list.indexOf(a) - list.indexOf(b),
19
+ );
20
+ }
package/src/surface.ts CHANGED
@@ -10,13 +10,16 @@ import { EditorState } from '@tiptap/pm/state';
10
10
  import type { LitElement } from 'lit';
11
11
  import { createTiptapAdapter } from './adapter.js';
12
12
  import { BLOCK_CHANGE, type SurfaceChange } from './blocks.js';
13
- import { astrolabeExtensions, PAGE_CHANGE, type PictureHooks } from './nodes.js';
13
+ import { astrolabeExtensions, PAGE_CHANGE, type PictureHooks, setPageActions } from './nodes.js';
14
+ import type { PageAction } from './page-actions.js';
14
15
  import { adoptSurfaceStyles, SURFACE_CLASS } from './styles.js';
15
16
 
16
17
  export interface Surface {
17
18
  readonly adapter: EditorAdapter;
18
19
  /** The authoring its labels come from. */
19
20
  readonly authoring: Authoring;
21
+ /** Draws these buttons in the page heads from now on (`pageActions`). */
22
+ setPageActions(actions: readonly PageAction[]): void;
20
23
  /** Destroys the editor and removes its element. */
21
24
  destroy(): void;
22
25
  }
@@ -25,6 +28,7 @@ export interface Surface {
25
28
  * A TipTap editor on `source`, appended to `host` (the `<astl-editor>`) in the `surface` slot. `manifests` are every
26
29
  * registered type: the schema keeps them all, so content of a type the authoring leaves out is never dropped. `changes`
27
30
  * hears of each page added or joined and each block moved or deleted, before the change event of its edit.
31
+ * `pageActions` are the buttons of the page heads.
28
32
  */
29
33
  export function createSurface(
30
34
  host: LitElement,
@@ -33,6 +37,7 @@ export function createSurface(
33
37
  source: string,
34
38
  pictures?: PictureHooks,
35
39
  changes?: (change: SurfaceChange) => void,
40
+ pageActions?: readonly PageAction[],
36
41
  ): Surface {
37
42
  adoptSurfaceStyles(host);
38
43
  const mount = document.createElement('div');
@@ -64,7 +69,10 @@ export function createSurface(
64
69
  element: mount,
65
70
  // No <style> element: the surface stylesheet is adopted (strict CSP allows it).
66
71
  injectCSS: false,
67
- extensions: astrolabeExtensions(authoring, manifests, pictures === undefined ? {} : { pictures }),
72
+ extensions: astrolabeExtensions(authoring, manifests, {
73
+ ...(pictures === undefined ? {} : { pictures }),
74
+ ...(pageActions === undefined ? {} : { pageActions }),
75
+ }),
68
76
  editorProps: {
69
77
  attributes: (state) => ({
70
78
  'aria-label': authoring.t(EDITOR_NAMESPACE, 'surface.label'),
@@ -102,7 +110,8 @@ export function createSurface(
102
110
  const toolbar = host.renderRoot.querySelector('[part~="toolbar"]');
103
111
  if (toolbar !== null && editor?.isDestroyed === false) covered.observe(toolbar);
104
112
  });
105
- return { adapter, authoring, destroy };
113
+ const made = editor;
114
+ return { adapter, authoring, destroy, setPageActions: (actions) => setPageActions(made, actions) };
106
115
  } catch (error) {
107
116
  destroy();
108
117
  throw error;
package/src/tiptap.ts CHANGED
@@ -7,3 +7,5 @@
7
7
  export { createTiptapAdapter } from './adapter.js';
8
8
  export type { PictureHooks } from './nodes.js';
9
9
  export { astrolabeExtensions } from './nodes.js';
10
+ export type { PageAction } from './page-actions.js';
11
+ export { PAGE_ACTIONS } from './page-actions.js';