@astl/editor-tiptap 0.1.1 → 0.1.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"styles.js","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAkB,GAAG,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAErD,wGAAwG;AACxG,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,iBAAiB,GAAc,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAkH;AAClH,kDAAkD;AAClD,MAAM,UAAU,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmErB,CAAC;AAEF,yDAAyD;AACzD,MAAM,WAAW,GAAG,SAAS,CAC3B,KAAK,CAAC,IAAI,CACR,EAAE,MAAM,EAAE,CAAC,EAAE,EACb,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CACP,+BAA+B,CAAC,GAAG,CAAC,8BAA8B,CAAC,GAAG,CAAC,+BAA+B,CAAC,GAAG,CAAC,SAAS,CACvH,CAAC,IAAI,CAAC,EAAE,CAAC,CACX,CAAC;AAEF,sHAAsH;AACtH,yGAAyG;AACzG,MAAM,YAAY,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmDvB,CAAC;AAEF,MAAM,aAAa,GAAc,GAAG,CAAA,GAAG,iBAAiB,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,EAAE,CAAC;AAErG,2FAA2F;AAC3F,MAAM,UAAU,kBAAkB,CAAC,IAAU;IAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAmC,CAAC;IACjE,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,CAAC;IACvC,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,CAAC,kBAAkB,KAAK,SAAS;QAAE,OAAO;IACzE,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAC9G,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astl/editor-tiptap",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "<astl-editor>: the authoring kit around a TipTap surface, loaded only when no other editor is plugged in through an adapter.",
5
5
  "keywords": [
6
6
  "astrolabe",
@@ -53,11 +53,11 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@astl/core": "0.1.1",
57
- "@astl/editor": "0.1.1",
58
- "@astl/engine": "0.1.1",
59
- "@astl/player": "0.1.1",
60
- "@astl/tokens": "0.1.1",
56
+ "@astl/core": "0.1.3",
57
+ "@astl/editor": "0.1.3",
58
+ "@astl/engine": "0.1.3",
59
+ "@astl/player": "0.1.3",
60
+ "@astl/tokens": "0.1.3",
61
61
  "@tiptap/core": "3.32.0",
62
62
  "@tiptap/pm": "3.32.0",
63
63
  "@tiptap/starter-kit": "3.32.0",
package/src/element.ts CHANGED
@@ -406,7 +406,7 @@ const CHANGE = 'astl-editor-change';
406
406
  const READY = 'astl-editor-ready';
407
407
  const VIEW = 'astl-editor-view';
408
408
 
409
- /** The views of every editor: the surface, and the source as HTML. */
409
+ /** The views of every editor: the surface, and the source as HTML (not for a surface that is the source already). */
410
410
  const OWN_VIEWS: readonly string[] = ['visual', 'source'];
411
411
 
412
412
  /** The status line counts this long after the last change, as the issues list does. */
@@ -675,9 +675,14 @@ export class AstlEditor extends LitElement {
675
675
  return Array.isArray(views) ? views : [];
676
676
  }
677
677
 
678
+ /** The editor's own views: without `source` when the surface edits the source as text. */
679
+ #ownViews(): readonly string[] {
680
+ return this.adapter?.editsSource === true ? ['visual'] : OWN_VIEWS;
681
+ }
682
+
678
683
  /** The view on show: `view` when it names one, else `visual`. */
679
684
  #view(): string {
680
- const names = [...OWN_VIEWS, ...this.#hostViews().map((v) => v.name)];
685
+ const names = [...this.#ownViews(), ...this.#hostViews().map((v) => v.name)];
681
686
  return names.includes(this.view) ? this.view : 'visual';
682
687
  }
683
688
 
@@ -713,7 +718,8 @@ export class AstlEditor extends LitElement {
713
718
 
714
719
  protected override willUpdate(changed: PropertyValues<this>): void {
715
720
  if (changed.has('theme')) this.dataset.theme = this.#theme();
716
- if (changed.has('view')) {
721
+ // A surface that edits the source has no source view to leave or to load.
722
+ if (changed.has('view') && this.adapter?.editsSource !== true) {
717
723
  const before = changed.get('view');
718
724
  if (before === 'source' && this.view !== 'source' && !this.#leaveSource()) this.view = 'source';
719
725
  else if (this.view === 'source' && before !== 'source') {
@@ -896,8 +902,13 @@ export class AstlEditor extends LitElement {
896
902
  : html`<astl-inspector part="inspector" exportparts=${INSPECTOR_PARTS} data-theme=${theme} .authoring=${authoring} .adapter=${adapter}></astl-inspector>`;
897
903
  const t = (key: string, params?: MessageParams) => authoring.t(EDITOR_NAMESPACE, key, params);
898
904
  const view = this.#view();
905
+ // A surface that edits the source as text is the HTML already: its one view takes the HTML view's name and
906
+ // description, as it makes that view needless.
899
907
  const views: ToolbarView[] = [
900
- ...OWN_VIEWS.map((name) => ({ name, label: t(`view.${name}`) })),
908
+ ...this.#ownViews().map((name) => {
909
+ const shown = adapter?.editsSource === true ? 'source' : name;
910
+ return { name, label: t(`view.${shown}`), description: t(`view.${shown}Hint`) };
911
+ }),
901
912
  ...this.#hostViews(),
902
913
  ];
903
914
  // The toolbar spans the editor, as a word processor's: above the text and the side column.
package/src/nodes.ts CHANGED
@@ -617,6 +617,19 @@ function keptHelp(children: readonly Kept[], authoring: Authoring): HTMLElement[
617
617
  /** Costs the select offers, as written in `cost` ('0': free, no attribute). */
618
618
  const COSTS: readonly string[] = ['0', '0.1', '0.25', '0.5', '1'];
619
619
 
620
+ /** Takes the help box at `pos` away in one undo step; a page or a column it leaves empty keeps an empty paragraph. */
621
+ function removeHelp(editor: Editor, pos: number): void {
622
+ const { state } = editor;
623
+ const box = state.doc.nodeAt(pos);
624
+ const paragraph = state.schema.nodes.paragraph;
625
+ if (box === null || paragraph === undefined) return;
626
+ const tr = closeHistory(state.tr);
627
+ if (state.doc.resolve(pos).parent.childCount === 1)
628
+ tr.replaceWith(pos, pos + box.nodeSize, paragraph.create());
629
+ else tr.delete(pos, pos + box.nodeSize);
630
+ editor.view.dispatch(tr.scrollIntoView());
631
+ }
632
+
620
633
  function helpNode(tag: string, authoring?: Authoring): Node {
621
634
  const name = nodeName(tag);
622
635
  const t = (key: string, params?: MessageParams) => authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
@@ -629,6 +642,21 @@ function helpNode(tag: string, authoring?: Authoring): Node {
629
642
  addAttributes: () => sourceAttributes,
630
643
  parseHTML: () => [{ tag }],
631
644
  renderHTML: ({ node }) => [tag, { ...node.attrs.attributes }, 0],
645
+ addKeyboardShortcuts() {
646
+ // Isolating keeps Backspace and Delete in the box; in an empty box (one empty line) they take it away.
647
+ const removeEmpty = ({ editor }: { editor: Editor }) => {
648
+ const { selection } = editor.state;
649
+ const { $from } = selection;
650
+ if (!selection.empty || $from.depth < 2) return false;
651
+ const box = $from.node(-1);
652
+ const line = box.firstChild;
653
+ if (box.type.name !== name || box.childCount !== 1 || !line?.isTextblock || line.content.size > 0)
654
+ return false;
655
+ removeHelp(editor, $from.before(-1));
656
+ return true;
657
+ };
658
+ return { Backspace: removeEmpty, Delete: removeEmpty };
659
+ },
632
660
  addNodeView:
633
661
  () =>
634
662
  ({ node, editor, getPos }) => {
@@ -663,6 +691,18 @@ function helpNode(tag: string, authoring?: Authoring): Node {
663
691
  else attributes[tag === HINT_TAG ? 'cost' : 'when'] = select.value;
664
692
  editor.view.dispatch(editor.state.tr.setNodeAttribute(pos, 'attributes', attributes));
665
693
  });
694
+ // "Remove" at the end of the head, named by the kind of box: a box the menu added empty has no other way out.
695
+ const remove = head.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
696
+ remove.className = 'astl-help-remove';
697
+ remove.textContent = t('inspector.remove');
698
+ remove.setAttribute('aria-label', t('surface.helpRemove', { kind: tag.replace(/^astl-/, '') }));
699
+ remove.addEventListener('click', () => {
700
+ const pos = getPos();
701
+ if (typeof pos !== 'number') return;
702
+ removeHelp(editor, pos);
703
+ editor.view.focus();
704
+ });
705
+ remove.addEventListener('keydown', (event) => historyKey(editor.view, event));
666
706
  const content = dom.appendChild(document.createElement('div'));
667
707
  content.className = 'astl-help-content';
668
708
  const render = (attrs: Record<string, unknown>) => {
@@ -724,6 +764,8 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
724
764
  const repeated = manifest.children.find(
725
765
  (c) => c.content === 'inline' && (c.max === null || c.max > 1) && flags?.includes(c.tag) === true,
726
766
  );
767
+ // The rich child a block has once (a prompt): a block without one gets it from a button too.
768
+ const single = manifest.children.find((c) => c.content === 'inline' && c.max === 1);
727
769
  return Node.create({
728
770
  name,
729
771
  group: 'block',
@@ -752,12 +794,34 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
752
794
  // The picture under the task (and an image drop's labels), as the student sees them.
753
795
  dom.append(contentDOM, ...(picture === null ? [] : [picture.dom]));
754
796
  let add: HTMLButtonElement | null = null;
755
- if (repeated !== undefined && authoring !== undefined) {
756
- const childType = nodeName(repeated.tag);
757
- const row = dom.appendChild(document.createElement('div'));
797
+ let addSingle: HTMLButtonElement | null = null;
798
+ const row =
799
+ (repeated !== undefined || single !== undefined) && authoring !== undefined
800
+ ? dom.appendChild(document.createElement('div'))
801
+ : null;
802
+ if (row !== null && authoring !== undefined) {
758
803
  row.className = 'astl-block-add';
759
804
  row.contentEditable = 'false';
760
805
  Object.assign(row, { lang: authoring.locale, dir: directionOf(authoring.locale) });
806
+ }
807
+ if (row !== null && single !== undefined && authoring !== undefined) {
808
+ // "Add prompt": at the block's start, the cursor in it; the button hides while the block has one.
809
+ const singleType = nodeName(single.tag);
810
+ addSingle = Object.assign(document.createElement('button'), { type: 'button' });
811
+ addSingle.append(lineIcon('add', 'astl-icon'), authoring.t(EDITOR_NAMESPACE, 'surface.addPrompt'));
812
+ addSingle.addEventListener('click', () => {
813
+ const pos = getPos();
814
+ const type = editor.schema.nodes[singleType];
815
+ if (typeof pos !== 'number' || type === undefined) return;
816
+ const tr = closeHistory(editor.state.tr).insert(pos + 1, type.create());
817
+ tr.setSelection(TextSelection.create(tr.doc, pos + 2)).scrollIntoView();
818
+ editor.view.dispatch(tr);
819
+ editor.view.focus();
820
+ });
821
+ addSingle.addEventListener('keydown', (event) => historyKey(editor.view, event));
822
+ }
823
+ if (row !== null && repeated !== undefined && authoring !== undefined) {
824
+ const childType = nodeName(repeated.tag);
761
825
  add = row.appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
762
826
  add.append(lineIcon('add', 'astl-icon'), authoring.t(EDITOR_NAMESPACE, 'surface.addOption'));
763
827
  add.addEventListener('click', () => {
@@ -777,6 +841,7 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
777
841
  });
778
842
  add.addEventListener('keydown', (event) => historyKey(editor.view, event));
779
843
  }
844
+ if (row !== null && addSingle !== null) row.append(addSingle);
780
845
  const showPlace = authoring === undefined ? null : componentPlacing(authoring, dom, editor, getPos);
781
846
  // The picture draws the block's children too: every change of the block reaches it.
782
847
  const render = (next: PMNode) => {
@@ -790,13 +855,20 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
790
855
  });
791
856
  add.disabled = repeated.max !== null && count >= repeated.max;
792
857
  }
858
+ if (addSingle !== null && single !== undefined) {
859
+ let has = false;
860
+ next.forEach((child) => {
861
+ if (child.type.name === nodeName(single.tag)) has = true;
862
+ });
863
+ addSingle.hidden = has;
864
+ }
793
865
  };
794
866
  render(node);
795
867
  const tools = (target: globalThis.Node | null) =>
796
868
  (target as Element | null)?.closest?.('.astl-picture-handle') != null;
797
869
  const own = (target: globalThis.Node) =>
798
870
  (picture?.dom.contains(target) ?? false) ||
799
- (add?.parentElement?.contains(target) ?? false) ||
871
+ (row?.contains(target) ?? false) ||
800
872
  tools(target instanceof Element ? target : target.parentElement);
801
873
  return {
802
874
  dom,
@@ -807,7 +879,7 @@ function blockNode(manifest: ComponentManifest, pictures?: PictureView, authorin
807
879
  return true;
808
880
  },
809
881
  stopEvent: (event) =>
810
- (add?.parentElement?.contains(event.target as globalThis.Node) ?? false) ||
882
+ (row?.contains(event.target as globalThis.Node) ?? false) ||
811
883
  tools(event.target as globalThis.Node),
812
884
  // A drag of the handle writes the frame's width as it goes.
813
885
  ignoreMutation: (m) => own(m.target) || (m.target === dom && m.type === 'attributes'),
@@ -1882,8 +1954,9 @@ const specOf = (manifest: ComponentManifest, child: PMNode): ChildSpec | undefin
1882
1954
  manifest.children.find((c) => nodeName(c.tag) === child.type.name);
1883
1955
 
1884
1956
  /**
1885
- * Children of a rich block keep local ids of their own: a child without one (just added) or with one an earlier child
1886
- * has (a split or pasted copy) gets a fresh one, `<tag>-<n>`, and none of the answer attributes of the child it copies.
1957
+ * Children of a rich block keep local ids of their own: a child without one (just added, or loaded from a source that
1958
+ * has none) or with one an earlier child has (a split or pasted copy) gets a fresh one, `<tag>-<n>`. Only a copy loses
1959
+ * its answer attributes (the child it copies keeps them): a loaded child without an id keeps its `correct`.
1887
1960
  */
1888
1961
  function uniqueLocalIds(
1889
1962
  blocks: ReadonlyMap<string, ComponentManifest>,
@@ -1911,11 +1984,14 @@ function uniqueLocalIds(
1911
1984
  continue;
1912
1985
  }
1913
1986
  if (value === '' && !required) continue;
1914
- next ??= Object.fromEntries(
1915
- Object.entries(attributes).filter(
1916
- ([attr]) => spec?.attributes.find((a) => a.name === attr)?.visibility !== 'private',
1917
- ),
1918
- );
1987
+ next ??=
1988
+ value === ''
1989
+ ? { ...attributes }
1990
+ : Object.fromEntries(
1991
+ Object.entries(attributes).filter(
1992
+ ([attr]) => spec?.attributes.find((a) => a.name === attr)?.visibility !== 'private',
1993
+ ),
1994
+ );
1919
1995
  const prefix = spec?.tag.replace(/^astl-/, '') ?? 'item';
1920
1996
  let n = 1;
1921
1997
  while (taken.has(`${prefix}-${n}`)) n++;
@@ -1944,11 +2020,46 @@ function childrenExtension(manifests: readonly ComponentManifest[]): Extension {
1944
2020
  )
1945
2021
  .map((m) => [nodeName(m.tag), m]),
1946
2022
  );
2023
+ // The rich children of blocks (a prompt, an option), and those a block has once (a prompt), a rich block's lead too.
2024
+ const fields = manifests
2025
+ .filter((m) => m.display === 'block')
2026
+ .flatMap((m) => m.children.filter((c) => c.content === 'inline'));
2027
+ const children = new Set(fields.map((c) => nodeName(c.tag)));
2028
+ const singles = new Set(fields.filter((c) => c.max === 1).map((c) => nodeName(c.tag)));
1947
2029
  return Extension.create({
1948
2030
  name: 'astlChildren',
1949
2031
  priority: 1000,
1950
2032
  addKeyboardShortcuts() {
2033
+ /** What the last Ctrl/Cmd+A selected in a field: the next press, with nothing moved, selects everything. */
2034
+ let field: Selection | null = null;
1951
2035
  return {
2036
+ // Ctrl/Cmd+A in a prompt or an option selects its text, as in a form's field; a second press, the whole
2037
+ // exercise. Selecting it all at once, a Backspace emptied the exercise instead of the field.
2038
+ 'Mod-a': ({ editor }) => {
2039
+ const { selection, doc } = editor.state;
2040
+ const { $from, $to } = selection;
2041
+ const again = field !== null && selection.eq(field);
2042
+ field = null;
2043
+ if (again || $from.depth < 2 || !$from.sameParent($to) || !children.has($from.parent.type.name))
2044
+ return false;
2045
+ const text = TextSelection.create(doc, $from.start(), $from.end());
2046
+ editor.view.dispatch(editor.state.tr.setSelection(text));
2047
+ field = editor.state.selection;
2048
+ return true;
2049
+ },
2050
+ // An empty prompt goes on Backspace as on Delete (the block is isolating, so Backspace kept it), when the block
2051
+ // has other children: the cursor goes to the start of the next one.
2052
+ Backspace: ({ editor }) => {
2053
+ const { selection } = editor.state;
2054
+ const { $from } = selection;
2055
+ if (!selection.empty || $from.depth < 2 || !singles.has($from.parent.type.name)) return false;
2056
+ if ($from.parent.content.size > 0 || $from.node(-1).childCount < 2) return false;
2057
+ const start = $from.before();
2058
+ const tr = editor.state.tr.delete(start, $from.after());
2059
+ tr.setSelection(Selection.near(tr.doc.resolve(start)));
2060
+ editor.view.dispatch(tr.scrollIntoView());
2061
+ return true;
2062
+ },
1952
2063
  Enter: ({ editor }) => {
1953
2064
  const { state } = editor;
1954
2065
  const { $from } = state.selection;
package/src/styles.ts CHANGED
@@ -173,11 +173,18 @@ const baseSurfaceStyles: CSSResult = css`
173
173
  }
174
174
  .astl-surface .astl-page-label:focus-visible,
175
175
  .astl-surface
176
- :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title):focus-visible {
176
+ :is(
177
+ .astl-page-join,
178
+ .astl-page-delete,
179
+ .astl-page-add-title,
180
+ .astl-page-remove-title,
181
+ .astl-help-remove
182
+ ):focus-visible {
177
183
  outline: var(--astl-focus-width) solid var(--astl-focus);
178
184
  outline-offset: var(--astl-focus-offset);
179
185
  }
180
- .astl-surface :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title) {
186
+ .astl-surface
187
+ :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title, .astl-help-remove) {
181
188
  min-block-size: var(--astl-target-min);
182
189
  min-inline-size: var(--astl-target-min);
183
190
  padding-inline: var(--astl-space-3);
@@ -192,6 +199,10 @@ const baseSurfaceStyles: CSSResult = css`
192
199
  .astl-surface :is(.astl-page-join, .astl-page-delete, .astl-page-add-title, .astl-page-remove-title)[hidden] {
193
200
  display: none;
194
201
  }
202
+ /* Beside the title the button keeps its words whole (it broke "Remove" at 320 px): the title gives way instead. */
203
+ .astl-surface .astl-page-remove-title {
204
+ flex: none;
205
+ }
195
206
 
196
207
  /* A component in text (a gap): a chip with an icon, its answer and a badge counting the other answers, a long word
197
208
  broken inside it rather than past the column. Without an answer it says so, dashed and with a warning icon: never
@@ -535,6 +546,11 @@ const baseSurfaceStyles: CSSResult = css`
535
546
  outline: var(--astl-focus-width) solid var(--astl-focus);
536
547
  outline-offset: var(--astl-focus-offset);
537
548
  }
549
+ /* "Remove" closes the head, a page button's look. */
550
+ .astl-surface .astl-help-remove {
551
+ margin-inline-start: auto;
552
+ font-weight: 400;
553
+ }
538
554
  .astl-surface .astl-help-content > :first-child {
539
555
  margin-block-start: var(--astl-space-1);
540
556
  }
@@ -603,9 +619,18 @@ const baseSurfaceStyles: CSSResult = css`
603
619
  .astl-surface .astl-child[data-flagged] {
604
620
  background: var(--astl-success-soft);
605
621
  }
622
+ /* "Add option" and "Add prompt" side by side; a hidden one (the block has its prompt) takes no room, nor does a row
623
+ with nothing to add. */
606
624
  .astl-surface .astl-block-add {
625
+ display: flex;
626
+ flex-wrap: wrap;
627
+ gap: var(--astl-space-2);
607
628
  padding-block-start: var(--astl-space-1);
608
629
  }
630
+ .astl-surface .astl-block-add button[hidden],
631
+ .astl-surface .astl-block-add:not(:has(button:not([hidden]))) {
632
+ display: none;
633
+ }
609
634
  .astl-surface .astl-block-add button {
610
635
  padding-inline: var(--astl-space-2) var(--astl-space-3);
611
636
  color: var(--astl-accent);
@@ -630,12 +655,26 @@ const baseSurfaceStyles: CSSResult = css`
630
655
  background: var(--astl-accent-soft);
631
656
  }
632
657
 
633
- /* The selected atom or block gets the focus ring. */
658
+ /* The selected atom or block gets the focus ring. A chip sits in running text, where a ring around it covers the
659
+ words beside it: its ring is inside it, and it is filled (its border is the accent already). An empty chip keeps
660
+ its colours: the solid ring stands out on its dashed one. */
634
661
  .astl-surface .ProseMirror-selectednode {
635
662
  outline: var(--astl-focus-width) solid var(--astl-focus);
636
663
  outline-offset: var(--astl-focus-offset);
637
664
  }
665
+ .astl-surface .astl-chip.ProseMirror-selectednode {
666
+ outline-offset: calc(-1 * var(--astl-focus-width));
667
+ }
668
+ .astl-surface .astl-chip.ProseMirror-selectednode:not([data-empty]) {
669
+ background: var(--astl-accent);
670
+ color: var(--astl-on-accent);
671
+ }
638
672
  @media (forced-colors: active) {
673
+ .astl-surface .astl-chip.ProseMirror-selectednode:not([data-empty]) {
674
+ background: Highlight;
675
+ color: HighlightText;
676
+ forced-color-adjust: none;
677
+ }
639
678
  .astl-surface .astl-frame-current {
640
679
  --frame: Highlight;
641
680
  }