@astl/editor-tiptap 0.2.0 → 0.2.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/dist/nodes.js CHANGED
@@ -25,7 +25,7 @@ import { EDITOR_NAMESPACE, pictureHtml, pictureSize, positionOf, splitPlacement,
25
25
  import { Extension, Mark, Node } from '@tiptap/core';
26
26
  import { closeHistory } from '@tiptap/pm/history';
27
27
  import { DOMSerializer, Fragment, Slice, } from '@tiptap/pm/model';
28
- import { NodeSelection, Plugin, Selection, TextSelection, } from '@tiptap/pm/state';
28
+ import { NodeSelection, Plugin, PluginKey, Selection, TextSelection, } from '@tiptap/pm/state';
29
29
  import { Decoration, DecorationSet } from '@tiptap/pm/view';
30
30
  import { StarterKit } from '@tiptap/starter-kit';
31
31
  import { blocksExtension, historyKey, LAYOUT, LAYOUT_COLUMN, placeLabel, } from './blocks.js';
@@ -266,7 +266,7 @@ function summary(manifest, attrs) {
266
266
  const main = manifest?.attributes.find((a) => a.type === 'string')?.name;
267
267
  if (main !== undefined)
268
268
  return attrs.attributes[main] ?? '';
269
- // Its help for students is not what it holds: the frame shows it apart (`keptHelp`).
269
+ // Its help for students is not what it holds: the frame's tags say it, the card edits it.
270
270
  const children = attrs.children.filter((c) => typeof c === 'string' || !HELP_TAGS.has(c.tag));
271
271
  const elements = children.filter((c) => typeof c !== 'string');
272
272
  return elements.length > 0
@@ -303,6 +303,7 @@ const SVG_ICONS = {
303
303
  check: 'M4 10.5l4 4 8-9',
304
304
  warning: 'M10 3l8 14H2zM10 8v4M10 14.5v.5',
305
305
  add: 'M10 4v12M4 10h12',
306
+ remove: 'M5 5l10 10M15 5L5 15',
306
307
  };
307
308
  /** A 20 by 20 line icon, hidden from assistive technology. */
308
309
  function lineIcon(name, className) {
@@ -406,9 +407,7 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
406
407
  dom.append(picture.dom);
407
408
  const body = dom.appendChild(document.createElement('div'));
408
409
  body.className = 'astl-atom-summary';
409
- // Its help for students, read only (the inspector edits it), under what it holds.
410
- const help = dom.appendChild(document.createElement('div'));
411
- help.className = 'astl-help-kept';
410
+ // Its help for students is not drawn: the frame's tags say what it has, the card edits it.
412
411
  // Inside the frame, not on it: an author's border must not hide what the frame says.
413
412
  const paint = placedPainter(body, tag);
414
413
  const showPlace = authoring === undefined ? null : componentPlacing(authoring, dom, editor, getPos);
@@ -419,8 +418,6 @@ function atomNode(tag, manifest, label, place, pictures, authoring) {
419
418
  // The picture shows the zones and pins; a summary of them would only repeat them.
420
419
  body.textContent = picture === null ? summary(manifest, attrs) : '';
421
420
  body.hidden = picture !== null;
422
- if (authoring !== undefined)
423
- help.replaceChildren(...keptHelp(attrs.children, authoring));
424
421
  };
425
422
  render(node.attrs);
426
423
  return {
@@ -445,36 +442,10 @@ const FLOW = `(${FLOW_NODES.join(' | ')})+`;
445
442
  /**
446
443
  * The group of the help for students (`<astl-hint>`, `<astl-explanation>`, `<astl-feedback>`): rich content in a page
447
444
  * or in a block component, never in a column or in text (an inline component or an atom keeps its own as data). In
448
- * the editor, a labelled box; its select sets a hint's cost or the verdict a feedback text is for.
445
+ * the editor, a page's is a labelled box; its select sets the verdict a feedback text is for. A block component's is
446
+ * kept, not drawn (the frame's tags say it, the card edits it): it cannot be selected, so no key reaches it unseen.
449
447
  */
450
448
  const HELP = 'help';
451
- /** The label of a help element in the UI language: what it is, with its cost or its verdict. */
452
- function helpLabel(tag, attributes, t) {
453
- if (tag === HINT_TAG) {
454
- const percent = Math.round(Number(attributes.cost ?? 0) * 100);
455
- return percent > 0 ? `${t('insert.hint')}, ${t('help.cost', { percent })}` : t('insert.hint');
456
- }
457
- if (tag === EXPLANATION_TAG)
458
- return t('insert.explanation');
459
- return `${t('insert.feedback')}: ${t(`help.when.${attributes.when ?? ''}`)}`;
460
- }
461
- /** An atom's help kept as data, as read-only labelled boxes. */
462
- function keptHelp(children, authoring) {
463
- const t = (key, params) => authoring.t(EDITOR_NAMESPACE, key, params);
464
- return children.flatMap((c) => {
465
- if (typeof c === 'string' || !HELP_TAGS.has(c.tag))
466
- return [];
467
- const box = document.createElement('div');
468
- box.className = 'astl-help-box';
469
- box.dataset.help = c.tag;
470
- const head = box.appendChild(document.createElement('div'));
471
- head.className = 'astl-help-head';
472
- head.lang = authoring.locale;
473
- head.textContent = helpLabel(c.tag, c.attributes, t);
474
- box.appendChild(document.createElement('div')).textContent = textOf(c).trim();
475
- return [box];
476
- });
477
- }
478
449
  /** Costs a question's hint offers, as written in `cost` ('0': free, no attribute); a cost besides them stays. */
479
450
  const COSTS = ['0', '0.1', '0.25', '0.5'];
480
451
  /** Whether help in `parent` is the page's: directly on a page, in a column, or in a document without pages. */
@@ -577,6 +548,7 @@ function helpNode(tag, authoring) {
577
548
  content: FLOW,
578
549
  defining: true,
579
550
  isolating: true,
551
+ selectable: false,
580
552
  addAttributes: () => sourceAttributes,
581
553
  parseHTML: () => [{ tag }],
582
554
  renderHTML: ({ node }) => [tag, { ...node.attrs.attributes }, 0],
@@ -693,11 +665,14 @@ function helpNode(tag, authoring) {
693
665
  attributes[tag === HINT_TAG ? 'cost' : 'when'] = select.value;
694
666
  editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', attributes));
695
667
  });
696
- // "Remove" at the end of the head, named by the kind of box: a box the menu added empty has no other way out.
668
+ // "Remove" at the end of the head, an icon named by the kind of box: a box the menu added empty has no other way
669
+ // out.
697
670
  const remove = head.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
698
671
  remove.className = 'astl-help-remove';
699
- remove.textContent = t('inspector.remove');
700
- remove.setAttribute('aria-label', t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') }));
672
+ remove.append(lineIcon('remove', 'astl-icon'));
673
+ const removeName = t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') });
674
+ remove.setAttribute('aria-label', removeName);
675
+ remove.title = removeName;
701
676
  remove.addEventListener('click', () => {
702
677
  const pos = getPos();
703
678
  if (typeof pos !== 'number')
@@ -782,6 +757,44 @@ function helpNode(tag, authoring) {
782
757
  export function helpNodes(authoring) {
783
758
  return [HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => helpNode(tag, authoring));
784
759
  }
760
+ /**
761
+ * An empty help box's line says what to write there ("Write the hint..."): the box keeps one empty paragraph to type
762
+ * in, and a decoration marks it (`astl-help-empty`, the text in `data-placeholder`, which the surface's style shows).
763
+ */
764
+ function helpPlaceholderExtension(authoring) {
765
+ const kinds = new Map([HINT_TAG, EXPLANATION_TAG, FEEDBACK_TAG].map((tag) => [nodeName(tag), tag.replace(/^astl-/, '')]));
766
+ const decorate = (doc) => {
767
+ const decorations = [];
768
+ doc.descendants((node, pos) => {
769
+ const kind = kinds.get(node.type.name);
770
+ if (kind === undefined)
771
+ return !node.isTextblock;
772
+ const line = node.firstChild;
773
+ if (node.childCount === 1 && line !== null && line.isTextblock && line.content.size === 0) {
774
+ decorations.push(Decoration.node(pos + 1, pos + 1 + line.nodeSize, {
775
+ class: 'astl-help-empty',
776
+ 'data-placeholder': authoring.t(EDITOR_NAMESPACE, 'surface.helpPlaceholder', { kind }),
777
+ }));
778
+ }
779
+ return false;
780
+ });
781
+ return DecorationSet.create(doc, decorations);
782
+ };
783
+ const key = new PluginKey('astlHelpPlaceholder');
784
+ return Extension.create({
785
+ name: 'astlHelpPlaceholder',
786
+ addProseMirrorPlugins: () => [
787
+ new Plugin({
788
+ key,
789
+ state: {
790
+ init: (_, state) => decorate(state.doc),
791
+ apply: (tr, previous) => (tr.docChanged ? decorate(tr.doc) : previous),
792
+ },
793
+ props: { decorations: (state) => key.getState(state) },
794
+ }),
795
+ ],
796
+ });
797
+ }
785
798
  /**
786
799
  * A block with rich children, or with rich content (`content: 'flow'`), framed and labelled in the editor, with its
787
800
  * picture when it has picture fields.
@@ -823,6 +836,9 @@ function blockNode(manifest, pictures, authoring) {
823
836
  addNodeView: () => ({ node, getPos, editor }) => {
824
837
  const dom = document.createElement('div');
825
838
  dom.className = 'astl-block';
839
+ // A graded one's help for students is not drawn (its frame's tags say it, the card edits it); an ungraded one
840
+ // shows help it should not hold, which the engine warns of, so it can be taken away.
841
+ dom.toggleAttribute('data-astl-graded', manifest.graded);
826
842
  const picture = pictures?.layers(manifest.tag).length ? pictures.create(manifest.tag) : null;
827
843
  const contentDOM = document.createElement('div');
828
844
  // Inside the frame, not on it: an author's border must not hide what the frame says.
@@ -2778,7 +2794,8 @@ export function astrolabeExtensions(authoring, manifests = authoring.engine.mani
2778
2794
  childrenExtension(manifests),
2779
2795
  richBlocksExtension(authoring, manifests),
2780
2796
  pastedIdsExtension(authoring, manifests, hooks),
2781
- blocksExtension(authoring, (node) => types.get(node.type.name) ?? null, nodeName, hooks?.style === undefined ? undefined : () => hooks.style?.('component')),
2797
+ blocksExtension(authoring, (node) => types.get(node.type.name) ?? null, nodeName, hooks?.style === undefined ? undefined : () => hooks.style?.('component'), hooks?.help === undefined ? undefined : (part) => hooks.help?.(part)),
2798
+ helpPlaceholderExtension(authoring),
2782
2799
  ];
2783
2800
  }
2784
2801
  //# sourceMappingURL=nodes.js.map