foldkit-mixins-builder 0.7.0 → 0.9.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/index.js CHANGED
@@ -6,8 +6,9 @@ import { Entity } from "foldkit-entity";
6
6
  import { Input } from "foldkit-form";
7
7
  import { Behavior, Capability, Layers as Layers$1, Slot, SlotView, Slots, Style } from "foldkit-mixins";
8
8
  import { FormView } from "foldkit-mixins-form";
9
- import { KeepInView, Measure, measured } from "foldkit-primitives/dom";
9
+ import { DragPreview, KeepInView, Measure, measured } from "foldkit-primitives/dom";
10
10
  import { EditableText, LiveAnnounce, PointerDrag, Targets, TreeNavigation } from "foldkit-primitives/interaction";
11
+ import { GRIP_PLACE_ATTRIBUTE, GripPlace } from "./gripPlace.js";
11
12
  import { createLazy, inertHtml } from "foldkit/html";
12
13
  import * as Submodel from "foldkit/submodel";
13
14
  import { builderWords, builderWords as builderWords$1, say, wordsOf } from "./words.js";
@@ -123,6 +124,24 @@ const BuilderSlots = Slots.define({
123
124
  selectionLabel: Slot.make({ capability: Capability.Base }),
124
125
  /** Over the node under the pointer, as the selection's box is placed. */
125
126
  hoverBox: Slot.make({ capability: Capability.Base }),
127
+ /**
128
+ * Over the selected node, placed as its box is, holding the handle that moves it:
129
+ * a pointer's press and move or a finger's hold drags the node, a tap or a click on
130
+ * the grip opens `nodeMenu`. It takes no pointer itself; `nodeGrip` does.
131
+ */
132
+ nodeHandle: Slot.make({ capability: Capability.Container }),
133
+ /** The grip: a button, always drawn for the selected node, so a finger has something to hold. Carries `data-builder-handle`. */
134
+ nodeGrip: Slot.make({ capability: Capability.Interactive }),
135
+ /** What the selected node can do without a drag: its commands (move, indent, duplicate, delete), a popover opened by the grip. */
136
+ nodeMenu: Slot.make({ capability: Capability.Container }),
137
+ nodeAction: Slot.make({ capability: Capability.Interactive }),
138
+ /**
139
+ * What a drag carries, following the pointer in the top layer for as long as the drag
140
+ * lasts, hidden from assistive technology and taking no pointer. Carries `data-drag-source`.
141
+ */
142
+ dragPreview: Slot.make({ capability: Capability.Base }),
143
+ dragPreviewLabel: Slot.make({ capability: Capability.Base }),
144
+ dragPreviewSummary: Slot.make({ capability: Capability.Base }),
126
145
  /** The live region the Builder's announcements are read from. */
127
146
  live: Slot.make({ capability: Capability.Base })
128
147
  });
@@ -160,6 +179,47 @@ const FrameDefaults = Style.forSlots(BuilderSlots)({
160
179
  insetInlineStart: "0"
161
180
  }),
162
181
  hoverBox: boxOver("hovered"),
182
+ nodeHandle: Style.compose(boxOver("selected"), Style.self({ anchorScope: "--builder-grip" })),
183
+ nodeGrip: Style.compose(Style.self({
184
+ position: "absolute",
185
+ anchorName: "--builder-grip",
186
+ insetBlockEnd: "100%",
187
+ insetInlineEnd: "0",
188
+ pointerEvents: "auto",
189
+ cursor: "grab",
190
+ minInlineSize: "1.5rem",
191
+ minBlockSize: "1.5rem",
192
+ touchAction: "none",
193
+ userSelect: "none",
194
+ WebkitUserSelect: "none",
195
+ WebkitTouchCallout: "none"
196
+ }), Style.media("(pointer: coarse)", {
197
+ minInlineSize: "2.75rem",
198
+ minBlockSize: "2.75rem"
199
+ }), Style.nest(":root[data-fk-drag] &", { pointerEvents: "none" }), Style.nest(`[data-${GRIP_PLACE_ATTRIBUTE}="inside"] &`, {
200
+ insetBlockEnd: "auto",
201
+ insetBlockStart: "0"
202
+ })),
203
+ nodeMenu: Style.compose(Style.self({
204
+ margin: "0",
205
+ positionAnchor: "--builder-grip",
206
+ positionArea: "block-end span-inline-start",
207
+ pointerEvents: "auto"
208
+ }), Style.nest("&:not(:popover-open)", { display: "none !important" })),
209
+ nodeAction: Style.media("(pointer: coarse)", { minBlockSize: "2.75rem" }),
210
+ dragPreview: Style.compose(Style.self({
211
+ position: "fixed",
212
+ top: "0",
213
+ left: "0",
214
+ margin: "0",
215
+ padding: "0",
216
+ border: "0",
217
+ overflow: "visible",
218
+ background: "transparent",
219
+ pointerEvents: "none",
220
+ willChange: "transform",
221
+ transform: "translate(calc(var(--fk-drag-x, 0px) + 12px), calc(var(--fk-drag-y, 0px) + 12px))"
222
+ }), Style.nest(":root[data-fk-drag=\"touch\"] &", { transform: "translate(calc(var(--fk-drag-x, 0px) - 50%), calc(var(--fk-drag-y, 0px) - 72px))" })),
163
223
  root: Style.self({
164
224
  containerType: "inline-size",
165
225
  containerName: "builder"
@@ -210,6 +270,22 @@ const ROW_ATTRIBUTE = "builder-row";
210
270
  const ROW_DROP_ATTRIBUTE = "builder-drop";
211
271
  /** On the row being dragged. */
212
272
  const ROW_DRAGGING_ATTRIBUTE = "builder-dragging";
273
+ /** On the selected node's grip, holding its node's id, for a drag to find. */
274
+ const HANDLE_ATTRIBUTE = "builder-handle";
275
+ /**
276
+ * What the DOM's drag facts say of where the pointer is, in the Builder's terms:
277
+ * `null` is over nothing, `region` too is over the page's own space, and a
278
+ * `part` is a node's empty Region.
279
+ */
280
+ const aimOf = (fact) => fact.over !== null ? fact.over.part === void 0 ? {
281
+ _tag: "OverNode",
282
+ id: NodeId.make(fact.over.id),
283
+ zone: fact.over.zone
284
+ } : {
285
+ _tag: "OverRegion",
286
+ id: NodeId.make(fact.over.id),
287
+ region: fact.over.part
288
+ } : fact.region === true ? { _tag: "OverPage" } : { _tag: "OverNothing" };
213
289
  /**
214
290
  * A drag's facts as the Builder's Messages; `source` says what the dragged
215
291
  * element is: a node's own id, or a palette tile's Block.
@@ -217,17 +293,30 @@ const ROW_DRAGGING_ATTRIBUTE = "builder-dragging";
217
293
  const dragMessage = (source) => (fact) => {
218
294
  switch (fact._tag) {
219
295
  case "DragStarted": return Message.DragStarted({ source: source(fact.id) });
220
- case "DraggedOver": return fact.over !== null ? fact.over.part === void 0 ? Message.DraggedOver({
221
- id: NodeId.make(fact.over.id),
222
- zone: fact.over.zone
223
- }) : Message.DraggedOverRegion({
224
- id: NodeId.make(fact.over.id),
225
- region: fact.over.part
226
- }) : fact.region === true ? Message.DraggedOverPage() : Message.DraggedOff();
227
- case "DragDropped": return Message.DragDropped();
296
+ case "DraggedOver": return Match.valueTags(aimOf(fact), {
297
+ OverNode: ({ id, zone }) => Message.DraggedOver({
298
+ id,
299
+ zone
300
+ }),
301
+ OverRegion: ({ id, region }) => Message.DraggedOverRegion({
302
+ id,
303
+ region
304
+ }),
305
+ OverPage: () => Message.DraggedOverPage(),
306
+ OverNothing: () => Message.DraggedOff()
307
+ });
308
+ case "DragDropped": return Message.DragDropped({ aim: aimOf(fact) });
228
309
  case "DragCancelled": return Message.DragCancelled();
229
310
  }
230
311
  };
312
+ /** Where a drag from outside the page lands: the canvas's nodes and the layers' rows, which share ids. */
313
+ const pageTargets = [{
314
+ attribute: `data-${NODE_ATTRIBUTE}`,
315
+ within: "[data-builder-canvas]"
316
+ }, {
317
+ attribute: `data-${ROW_ATTRIBUTE}`,
318
+ within: "[data-panel=\"layers\"] [role=\"tree\"]"
319
+ }];
231
320
  /** A row or a node dragged: a node on the page. */
232
321
  const nodeDrag = dragMessage((id) => ({
233
322
  _tag: "Existing",
@@ -584,13 +673,17 @@ const BuilderView = {
584
673
  ...extra
585
674
  ]), [label]);
586
675
  /** A command as a button: disabled while it has nothing to do, titled with its first key. */
587
- const commandButton = (h, slot, command, input) => {
676
+ const commandButton = (h, slot, command, input, extra = []) => {
588
677
  const [key] = command.keys;
589
678
  const w = wordsOf(input.words);
590
- return button(h, slot, command.label, command.run(input), [h.DataAttribute("action", command.id), h.Title(key === void 0 ? command.label : say(w.withKeys, {
591
- label: command.label,
592
- keys: keysOf(key, input.platform, w)
593
- }))]);
679
+ return button(h, slot, command.label, command.run(input), [
680
+ h.DataAttribute("action", command.id),
681
+ ...extra,
682
+ h.Title(key === void 0 ? command.label : say(w.withKeys, {
683
+ label: command.label,
684
+ keys: keysOf(key, input.platform, w)
685
+ }))
686
+ ]);
594
687
  };
595
688
  const labelAt = (document, id) => Option.match(Option.fromUndefinedOr(document.nodes[id]), {
596
689
  onNone: () => id,
@@ -611,6 +704,23 @@ const BuilderView = {
611
704
  }
612
705
  });
613
706
  const dropOf = (drag) => Option.flatMap(drag, (each) => Option.isSome(each.at) ? each.over : Option.none());
707
+ /**
708
+ * What a drag under way says of where it is: on `data-drag-place`, `here` where a drop would
709
+ * land and `nowhere` where it would be refused or is over nothing, so a stylist can show both.
710
+ * Absent while nothing is carried.
711
+ */
712
+ const placeMark = (drag, h) => Option.match(drag, {
713
+ onNone: () => [],
714
+ onSome: (each) => [h.DataAttribute("drag-place", Option.isSome(each.at) ? "here" : "nowhere")]
715
+ });
716
+ /**
717
+ * The end of the page, or of the layers, as a drop place: a drag over the page's own space,
718
+ * over no node, where it would land last. A node's own place is marked on the node.
719
+ */
720
+ const endMark = (drag, h) => Option.match(drag, {
721
+ onNone: () => [],
722
+ onSome: (each) => Option.isSome(each.at) && Option.isNone(each.over) ? [h.DataAttribute(ROW_DROP_ATTRIBUTE, "end")] : []
723
+ });
614
724
  const Shortcuts = Behavior.forSlots(BuilderSlots)({
615
725
  layers: Behavior.slot({ attributes: shortcuts }),
616
726
  canvas: Behavior.slot({ attributes: shortcuts })
@@ -646,10 +756,9 @@ const BuilderView = {
646
756
  behaviors: [panelMarks("insert", "palette"), PointerDrag.behavior(BuilderSlots)({
647
757
  container: "palette",
648
758
  attribute: "data-block",
649
- targets: {
650
- attribute: `data-${NODE_ATTRIBUTE}`,
651
- within: "[data-builder-canvas]"
652
- },
759
+ edgeScroll: {},
760
+ targets: pageTargets,
761
+ gaps: true,
653
762
  part: `data-${REGION_ATTRIBUTE}`,
654
763
  toMessage: tileDrag
655
764
  })]
@@ -717,6 +826,7 @@ const BuilderView = {
717
826
  onSome: (summary) => [h.span(slots.rowSummary.attrs(), [summary])]
718
827
  })
719
828
  ]);
829
+ const GripPlacement = Behavior.forSlots(BuilderSlots)({ nodeHandle: Behavior.slot({ mount: () => GripPlace({}) }) }, { name: "GripPlace" });
720
830
  const MeasureMarks = Behavior.forSlots(BuilderSlots)({ canvas: Behavior.slot({ mount: () => Measure({ targets: {
721
831
  selected: `[data-${MARK_ATTRIBUTE}="selected"] > *`,
722
832
  hovered: `[data-${MARK_ATTRIBUTE}="hovered"] > *`
@@ -742,6 +852,7 @@ const BuilderView = {
742
852
  PointerDrag.behavior(BuilderSlots)({
743
853
  container: "tree",
744
854
  attribute: `data-${ROW_ATTRIBUTE}`,
855
+ edgeScroll: {},
745
856
  toMessage: nodeDrag
746
857
  }),
747
858
  Shortcuts,
@@ -756,7 +867,12 @@ const BuilderView = {
756
867
  New: () => Option.none()
757
868
  }));
758
869
  const shown = TreeNavigation.shown(rowsOf(document), input.layers, layersArgs);
759
- const tree = h.ul(slots.tree.attrs([h.Role("tree"), h.AriaLabel(wordsOf(input.words).layers)]), shown.map((row, index) => slots.row.lazy({
870
+ const tree = h.ul(slots.tree.attrs([
871
+ h.Role("tree"),
872
+ h.AriaLabel(wordsOf(input.words).layers),
873
+ ...placeMark(input.drag, h),
874
+ ...endMark(input.drag, h)
875
+ ]), shown.map((row, index) => slots.row.lazy({
760
876
  index,
761
877
  id: row.id
762
878
  }, drawRow, {
@@ -836,6 +952,45 @@ const BuilderView = {
836
952
  "narrow"
837
953
  ].map((viewport) => button(h, slots.viewport, wordsOf(input.words)[viewport], Option.some(Message.ViewportChosen({ viewport })), [h.DataAttribute("viewport", viewport), h.AriaPressed(input.viewport === viewport ? "true" : "false")]))));
838
954
  const context = fieldsOf(builder.catalog.context);
955
+ const Preview = Parts.part("Preview", { reads: ["preview", "words"] }, (input, slots, h) => Object.keys(context).length === 0 ? null : h.div(slots.preview.attrs([h.Role("group"), h.AriaLabel(wordsOf(input.words).previewAs)]), Object.entries(context).map(([key, schema]) => contextField(slots, h, {
956
+ id: `${builder.name}-preview-${key}`,
957
+ label: key,
958
+ schema,
959
+ current: Option.fromUndefinedOr(input.preview[key]),
960
+ blank: wordsOf(input.words).unset,
961
+ send: (value) => Option.match(value, {
962
+ onNone: () => Message.PreviewCleared({ key }),
963
+ onSome: (chosen) => Message.PreviewChosen({
964
+ key,
965
+ value: chosen
966
+ })
967
+ })
968
+ }))));
969
+ const Alert = Parts.part("Alert", { reads: ["refused"] }, (input, slots, h) => Option.match(input.refused, {
970
+ onNone: () => null,
971
+ onSome: (refused) => h.p(slots.alert.attrs([h.Role("alert")]), [refused.message])
972
+ }));
973
+ const menuId = `${builder.name}-node-menu`;
974
+ /**
975
+ * The selected node's grip and its commands. The grip is a button, so a tap or a
976
+ * click opens the commands, a popover the platform opens and closes; a hold or a
977
+ * move drags the node instead, and the click a drag ends with is swallowed.
978
+ */
979
+ const nodeHandle = (h, slots, input, id, block) => {
980
+ const w = wordsOf(input.words);
981
+ const label = labelOf(block);
982
+ return h.div(slots.nodeHandle.attrs([h.Key("node-handle")]), [h.button(slots.nodeGrip.attrs([
983
+ h.Type("button"),
984
+ h.AriaLabel(say(w.moveNode, { label })),
985
+ h.DataAttribute(HANDLE_ATTRIBUTE, id),
986
+ h.Attribute("popovertarget", menuId)
987
+ ]), []), h.div(slots.nodeMenu.attrs([
988
+ h.Id(menuId),
989
+ h.Attribute("popover", "auto"),
990
+ h.Role("group"),
991
+ h.AriaLabel(say(w.nodeActions, { label }))
992
+ ]), builder.commands.filter((command) => command.placement.includes("node")).map((command) => commandButton(h, slots.nodeAction, command, input, [h.Attribute("popovertarget", menuId), h.Attribute("popovertargetaction", "hide")])))]);
993
+ };
839
994
  return {
840
995
  Panels,
841
996
  Palette,
@@ -844,24 +999,8 @@ const BuilderView = {
844
999
  Toolbar,
845
1000
  Crumbs,
846
1001
  Viewports,
847
- Preview: Parts.part("Preview", { reads: ["preview", "words"] }, (input, slots, h) => Object.keys(context).length === 0 ? null : h.div(slots.preview.attrs([h.Role("group"), h.AriaLabel(wordsOf(input.words).previewAs)]), Object.entries(context).map(([key, schema]) => contextField(slots, h, {
848
- id: `${builder.name}-preview-${key}`,
849
- label: key,
850
- schema,
851
- current: Option.fromUndefinedOr(input.preview[key]),
852
- blank: wordsOf(input.words).unset,
853
- send: (value) => Option.match(value, {
854
- onNone: () => Message.PreviewCleared({ key }),
855
- onSome: (chosen) => Message.PreviewChosen({
856
- key,
857
- value: chosen
858
- })
859
- })
860
- })))),
861
- Alert: Parts.part("Alert", { reads: ["refused"] }, (input, slots, h) => Option.match(input.refused, {
862
- onNone: () => null,
863
- onSome: (refused) => h.p(slots.alert.attrs([h.Role("alert")]), [refused.message])
864
- })),
1002
+ Preview,
1003
+ Alert,
865
1004
  Canvas: Parts.part("Canvas", {
866
1005
  reads: [
867
1006
  "page",
@@ -872,6 +1011,7 @@ const BuilderView = {
872
1011
  "preview",
873
1012
  "data",
874
1013
  "editing",
1014
+ "platform",
875
1015
  "words"
876
1016
  ],
877
1017
  behaviors: [
@@ -894,6 +1034,17 @@ const BuilderView = {
894
1034
  container: "canvas",
895
1035
  attribute: `data-${NODE_ATTRIBUTE}`,
896
1036
  part: `data-${REGION_ATTRIBUTE}`,
1037
+ gaps: true,
1038
+ edgeScroll: {},
1039
+ toMessage: nodeDrag
1040
+ }),
1041
+ PointerDrag.behavior(BuilderSlots)({
1042
+ container: "nodeHandle",
1043
+ attribute: `data-${HANDLE_ATTRIBUTE}`,
1044
+ targets: pageTargets,
1045
+ gaps: true,
1046
+ part: `data-${REGION_ATTRIBUTE}`,
1047
+ edgeScroll: {},
897
1048
  toMessage: nodeDrag
898
1049
  }),
899
1050
  EditableText.behavior(BuilderSlots)({
@@ -916,7 +1067,8 @@ const BuilderView = {
916
1067
  }),
917
1068
  Shortcuts,
918
1069
  KeepSelectionInView,
919
- MeasureMarks
1070
+ MeasureMarks,
1071
+ GripPlacement
920
1072
  ]
921
1073
  }, (input, slots, h) => {
922
1074
  const document = builder.document(input);
@@ -926,9 +1078,14 @@ const BuilderView = {
926
1078
  h.DataAttribute("builder-canvas", ""),
927
1079
  h.Role("region"),
928
1080
  h.AriaLabel(wordsOf(input.words).canvas),
929
- h.Tabindex(0)
1081
+ h.Tabindex(0),
1082
+ ...placeMark(input.drag, h)
930
1083
  ]), [
931
- h.div(slots.frame.attrs([h.DataAttribute("viewport", input.viewport), h.Style({ "--fk-frame-width": viewportWidths[input.viewport] })]), [...document.roots.length === 0 ? [h.p(slots.empty.attrs(), [wordsOf(input.words).emptyPage])] : [], ...Renderer.render(builder.renderer, document, inertHtml, {
1084
+ h.div(slots.frame.attrs([
1085
+ h.DataAttribute("viewport", input.viewport),
1086
+ ...endMark(input.drag, h),
1087
+ h.Style({ "--fk-frame-width": viewportWidths[input.viewport] })
1088
+ ]), [...document.roots.length === 0 ? [h.p(slots.empty.attrs(), [wordsOf(input.words).emptyPage])] : [], ...Renderer.render(builder.renderer, document, inertHtml, {
932
1089
  mode: "edit",
933
1090
  selected: input.selected,
934
1091
  hovered: input.hovered,
@@ -941,9 +1098,50 @@ const BuilderView = {
941
1098
  h.div(slots.selectionBox.attrs([h.AriaHidden(true)]), Option.match(selectedBlock, {
942
1099
  onNone: () => [],
943
1100
  onSome: (name) => [h.span(slots.selectionLabel.attrs(), [labelOf(name)])]
944
- }))
1101
+ })),
1102
+ ...Option.match(Option.all({
1103
+ id: input.selected,
1104
+ block: selectedBlock
1105
+ }), {
1106
+ onNone: () => [],
1107
+ onSome: ({ id, block }) => [nodeHandle(h, slots, input, id, block)]
1108
+ })
945
1109
  ]);
946
1110
  }),
1111
+ Carried: Parts.part("Carried", {
1112
+ reads: ["page", "drag"],
1113
+ behaviors: [Behavior.forSlots(BuilderSlots)({ dragPreview: Behavior.slot({ mount: () => DragPreview({}) }) }, { name: "DragPreview" })]
1114
+ }, (input, slots, h) => Option.match(input.drag, {
1115
+ onNone: () => null,
1116
+ onSome: ({ source }) => {
1117
+ const document = builder.document(input);
1118
+ const { kind, block, summary } = Match.valueTags(source, {
1119
+ Existing: ({ id }) => {
1120
+ const node = document.nodes[id];
1121
+ return {
1122
+ kind: "node",
1123
+ block: node === void 0 ? "?" : labelOf(node.block),
1124
+ summary: node === void 0 ? Option.none() : summaryOf(node)
1125
+ };
1126
+ },
1127
+ New: ({ block: name }) => ({
1128
+ kind: "new",
1129
+ block: labelOf(name),
1130
+ summary: Option.none()
1131
+ })
1132
+ });
1133
+ return h.div(slots.dragPreview.attrs([
1134
+ h.Key("drag-preview"),
1135
+ h.AriaHidden(true),
1136
+ h.Attribute("popover", "manual"),
1137
+ h.DataAttribute("drag-source", kind),
1138
+ ...placeMark(input.drag, h)
1139
+ ]), [h.span(slots.dragPreviewLabel.attrs(), [block]), ...Option.match(summary, {
1140
+ onNone: () => [],
1141
+ onSome: (text) => [h.span(slots.dragPreviewSummary.attrs(), [text])]
1142
+ })]);
1143
+ }
1144
+ })),
947
1145
  Live: Parts.part("Live", { reads: ["announcer"] }, (input, slots, h) => h.div(slots.live.attrs(), [LiveAnnounce.view(input.announcer, h)]))
948
1146
  };
949
1147
  },
@@ -989,10 +1187,11 @@ const BuilderView = {
989
1187
  h.div(slots.stage.attrs(), [draw(parts.Alert), draw(parts.Canvas)]),
990
1188
  h.div(slots.end.attrs(), [draw(parts.Inspector)])
991
1189
  ]),
1190
+ draw(parts.Carried),
992
1191
  draw(parts.Live)
993
1192
  ]));
994
1193
  }
995
1194
  };
996
- export { BuilderSlots, BuilderView, ROW_ATTRIBUTE, ROW_DRAGGING_ATTRIBUTE, ROW_DROP_ATTRIBUTE, builderWords, keysOf, layerId, rowsOf, viewportWidths };
1195
+ export { BuilderSlots, BuilderView, HANDLE_ATTRIBUTE, ROW_ATTRIBUTE, ROW_DRAGGING_ATTRIBUTE, ROW_DROP_ATTRIBUTE, builderWords, keysOf, layerId, rowsOf, viewportWidths };
997
1196
 
998
1197
  //# sourceMappingURL=index.js.map