@astl/editor-tiptap 0.3.3 → 0.4.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/dist/nodes.js CHANGED
@@ -30,6 +30,7 @@ import { Decoration, DecorationSet } from '@tiptap/pm/view';
30
30
  import { StarterKit } from '@tiptap/starter-kit';
31
31
  import { blocksExtension, CHECK_NODE, historyKey, LAYOUT, LAYOUT_COLUMN, placeLabel, } from './blocks.js';
32
32
  import { currentLayoutExtension, deleteLayoutIn, isEmptyLayout, layoutAt } from './layouts.js';
33
+ import { PAGE_ACTIONS, pageActionsOf } from './page-actions.js';
33
34
  import { inert } from './source.js';
34
35
  import { setPlacedStyle } from './style.js';
35
36
  /** `astl-choice` becomes `astlChoice`: ProseMirror content expressions only take word characters. */
@@ -1876,6 +1877,18 @@ const layoutNodes = [
1876
1877
  ];
1877
1878
  /** Page heads made so far: each gets ids of its own for its title field's name. */
1878
1879
  let pageHeads = 0;
1880
+ /** The page heads of an editor, in its storage under the page node's name. */
1881
+ const pageHeadsOf = (editor) => editor.storage[PAGE];
1882
+ /** Draws the page heads of `editor` with these buttons from now on (`<astl-editor>`'s `pageActions`). */
1883
+ export function setPageActions(editor, actions) {
1884
+ const heads = pageHeadsOf(editor);
1885
+ const next = pageActionsOf(actions);
1886
+ if (next.join(' ') === heads.actions.join(' '))
1887
+ return;
1888
+ heads.actions = next;
1889
+ for (const redraw of heads.redraws)
1890
+ redraw();
1891
+ }
1879
1892
  /**
1880
1893
  * `<astl-page>` with a head: the page's number of all (`surface.pageNumber`), "Add title" (`surface.pageAddTitle`: a
1881
1894
  * page has no title until it gets one), from the second page on a button that joins it to the page before
@@ -1883,10 +1896,11 @@ let pageHeads = 0;
1883
1896
  * the head in its field, named by the page's number (`surface.pageTitleOf`, placeholder `surface.pageTitle`), with
1884
1897
  * "Remove title". The field opens while the page has a label and while it has the focus, and closes when it loses the
1885
1898
  * focus empty, without touching the document. The buttons are named by their text and the number, the join button by
1886
- * the page before, so each control has a name of its own. A node view never learns its index: a plugin numbers the
1887
- * heads after every change of the document.
1899
+ * the page before, so each control has a name of its own, and each has `data-action` (`add-title`, `check`, `join`,
1900
+ * `delete`, `remove-title`). `actions` are the buttons drawn, in order: "Remove title" goes with "Add title". A node
1901
+ * view never learns its index: a plugin numbers the heads after every change of the document.
1888
1902
  */
1889
- export function pageNode(authoring) {
1903
+ export function pageNode(authoring, actions = PAGE_ACTIONS) {
1890
1904
  const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
1891
1905
  // UI texts in the content: in the UI language.
1892
1906
  const ui = { lang: authoring.locale, dir: directionOf(authoring.locale) };
@@ -1920,7 +1934,9 @@ export function pageNode(authoring) {
1920
1934
  parseHTML: () => [{ tag: 'astl-page' }],
1921
1935
  renderHTML: ({ node }) => ['astl-page', { ...node.attrs.attributes }, 0],
1922
1936
  addProseMirrorPlugins: () => [numbering],
1937
+ addStorage: () => ({ actions: pageActionsOf(actions), redraws: new Set() }),
1923
1938
  addNodeView: () => ({ node, getPos, editor }) => {
1939
+ const drawn = pageHeadsOf(editor);
1924
1940
  const dom = document.createElement('section');
1925
1941
  dom.className = 'astl-page';
1926
1942
  const head = document.createElement('div');
@@ -1942,6 +1958,7 @@ export function pageNode(authoring) {
1942
1958
  ...ui,
1943
1959
  });
1944
1960
  button.setAttribute('aria-labelledby', `${button.id} ${number.id}`);
1961
+ button.dataset.action = suffix;
1945
1962
  return button;
1946
1963
  };
1947
1964
  const addTitle = named('add-title', t('surface.pageAddTitle'));
@@ -1955,6 +1972,7 @@ export function pageNode(authoring) {
1955
1972
  hidden: true,
1956
1973
  ...ui,
1957
1974
  });
1975
+ join.dataset.action = 'join';
1958
1976
  const remove = named('delete', t('surface.pageDelete'));
1959
1977
  remove.hidden = true;
1960
1978
  const field = Object.assign(document.createElement('div'), {
@@ -1966,7 +1984,28 @@ export function pageNode(authoring) {
1966
1984
  placeholder: t('surface.pageTitle'),
1967
1985
  });
1968
1986
  const removeTitle = named('remove-title', t('surface.pageRemoveTitle'));
1969
- field.append(input, removeTitle);
1987
+ const buttons = {
1988
+ 'add-title': addTitle,
1989
+ check,
1990
+ join,
1991
+ delete: remove,
1992
+ };
1993
+ field.append(input);
1994
+ head.append(number, field);
1995
+ // What is not listed is not drawn at all; its adapter's method stays for the host's own controls. The number and
1996
+ // the field stay put, so a title being typed keeps the focus.
1997
+ const draw = () => {
1998
+ const shown = drawn.actions.map((action) => buttons[action]);
1999
+ for (const button of Object.values(buttons))
2000
+ if (!shown.includes(button))
2001
+ button.remove();
2002
+ field.before(...shown);
2003
+ if (drawn.actions.includes('add-title'))
2004
+ field.append(removeTitle);
2005
+ else
2006
+ removeTitle.remove();
2007
+ };
2008
+ drawn.redraws.add(draw);
1970
2009
  let attributes = node.attrs.attributes;
1971
2010
  // The author is in the field, or just asked for it: it stays while empty, till the focus leaves.
1972
2011
  let editing = false;
@@ -2048,7 +2087,7 @@ export function pageNode(authoring) {
2048
2087
  // As after a join: the editor takes the focus, the cursor at the start of the page it went to.
2049
2088
  editor.view.focus();
2050
2089
  });
2051
- head.append(number, addTitle, check, join, remove, field);
2090
+ draw();
2052
2091
  const contentDOM = document.createElement('div');
2053
2092
  // The page's look is for its content, not for the controls in its head.
2054
2093
  const paint = painter(contentDOM, 'astl-page');
@@ -2081,6 +2120,7 @@ export function pageNode(authoring) {
2081
2120
  // events and mutations alone.
2082
2121
  stopEvent: (event) => head.contains(event.target),
2083
2122
  ignoreMutation: (m) => head.contains(m.target),
2123
+ destroy: () => drawn.redraws.delete(draw),
2084
2124
  };
2085
2125
  },
2086
2126
  });
@@ -3118,6 +3158,12 @@ function referencesExtension(authoring, manifests) {
3118
3158
  ],
3119
3159
  });
3120
3160
  }
3161
+ /**
3162
+ * The TipTap extensions of the content format, labelled in `authoring`'s locale: the nodes of `manifests` (default: the
3163
+ * authoring's types), pages with their heads, pictures, layouts and help for students. `pictures` is how the surface
3164
+ * asks about pictures; `pageActions` are the buttons of each page's head, in order (default: `PAGE_ACTIONS`, all four),
3165
+ * read when the extensions are made.
3166
+ */
3121
3167
  export function astrolabeExtensions(authoring, manifests = authoring.engine.manifests, options = {}) {
3122
3168
  const hooks = options.pictures;
3123
3169
  const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
@@ -3140,7 +3186,7 @@ export function astrolabeExtensions(authoring, manifests = authoring.engine.mani
3140
3186
  ...formatMarks,
3141
3187
  contentAttributes,
3142
3188
  docNode,
3143
- pageNode(authoring),
3189
+ pageNode(authoring, options.pageActions),
3144
3190
  ...layoutNodes,
3145
3191
  currentLayoutExtension((node) => types.has(node.type.name)),
3146
3192
  rulesExtension,