tess_edit 0.7.0 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6e68c0d1066a8d679a86e312613c97af8e5904aaec3193e5ecf2857e28250cf
4
- data.tar.gz: c668e68b3b5bea838987ba68329a4fd9e40284a544fccc885b957ae11f3b3be5
3
+ metadata.gz: bdb23134586e655c6ed313efb59884341994544f205ed2aa9e6c6be74cafe2b5
4
+ data.tar.gz: 915e991ecb3b3d0a882141604d4a7a7ff84da57b35e1e7de57c993eb9acd6ca4
5
5
  SHA512:
6
- metadata.gz: 3554026076d8c3b3cca57df958ad473d8b2ad68154814053ed9fb7f59802974f71b2a6438f5527cae73b9aea4d276ba1d76922a1d8caf3e9407afa82579b34cb
7
- data.tar.gz: 275ffebd37f2dbb2fb4d536564880e664650d05b3d22de4f97b8b7d9c494211cd8d90517cb67a60c9ffa3b4089cbc8dc4c452410e1cf2d6b749bbfc013337b96
6
+ metadata.gz: 8e30a2ce09e7e435970925ff34bd84494af5c84623e6322ddc9a068bf69893ba0febc8c4427ad2b20a1007e7d5594cdc21e2042c3b5822dcb437be46ce3b0d8b
7
+ data.tar.gz: f695696d7baa42280c4421ff7b483af50143dad4e7a5611f72edcae7d34e50352a3f92b2579c5e7f6f70a4a3988a906cc206d3b4ceafe5f700e6b45ca41695be
data/README.md CHANGED
@@ -472,6 +472,52 @@ registerKit({
472
472
 
473
473
  `render` fills the panel's element once, when the editor is built; `editor` is the editor, with its `doc`, `select(id)`, `renderCanvas()` and its `element`, which sends `tess-edit:change`. The canvas is drawn again when the open panel changes, so it can show what the panel is about; a panel that changes what it shows calls `editor.renderCanvas()`. A panel dragged to the other pane moves its element, so Stimulus controllers in it disconnect and connect again: the same controller, so state kept in `initialize` survives it. The demo's Note kit has an Outline panel.
474
474
 
475
+ A panel can be in some of the kit's editors only, such as one showing the page's own form fields (a slot) where the page gives them, with `visible`; `side: "right"` puts it beside Inspect when the panels are split (otherwise it's on the left, after Layers); and `open` makes it the tab open at first, such as when its fields have an error:
476
+
477
+ ```js
478
+ panels: [{
479
+ key: "details", label: "Details", side: "right",
480
+ visible: (editor) => Boolean(editor.slot("details")), // decided once, when the editor is built
481
+ open: (editor) => "open" in editor.slot("details").dataset,
482
+ render: (element, editor) => { const slot = editor.slot("details"); element.append(slot); slot.hidden = false }
483
+ }]
484
+ ```
485
+
486
+ An arrangement stored for the kit (where tabs were dragged, remembered per kit, not per editor) that doesn't have a panel yet, or that was stored by an editor without it, gets it on its side, or in the other pane when that side has none, so it never makes a pane of its own. The demo's Note kit has a Details panel at `/?details=1` (`/?details=open` opens it).
487
+
488
+ A panel can change the document through the editor it's given, as Inspect does, each change an undo step:
489
+
490
+ - `editor.doc` is the document and `editor.selectedId` the selected block's id; `find(editor.doc, id).node` is a block and `fieldsFor(getBlock(node.type), editor.kit)` its Inspect fields in groups (`{ title, fields }`), such as a field with `type: "image"` or the key `attributes.href`, all imported from `tess_edit`; `getValue(node, key)` reads one.
491
+ - `editor.select(id)` selects a block.
492
+ - `editor.setValue(id, key, value)` sets a field, as typing in it does (at the breakpoint shown, for a responsive field; quick changes to the same field are one undo step, as typing is, unless `{ coalesce: false }` is passed as a fourth argument); `editor.place(id, { key: value, … })` sets several at once.
493
+ - `editor.insertNode(node, { parentId, index })` puts a new block (made with `createNode`) in a block, at the end without an index, and selects it; it returns null where it can't go (a lock, the blocks allowed, a block in it that doesn't fit).
494
+ - `editor.element` sends `tess-edit:change` after every change.
495
+
496
+ What is dragged from a panel can be dropped onto the canvas with the panel's `drop`, which takes it by the types the panel puts on the drag's `dataTransfer`. While it's dragged only those types can be read, so `over` says from them what letting go on a block would do: change that block (`{ replace: id }`, the block filled as it is when a file is dragged onto it), or insert a block of a type (`{ insert: "image" }`) where a Blocks tile of it would go, with the same line; null refuses it there. When it's let go, `onDrop` reads the data and gives the change: `{ values }` set on the block it replaces, or `{ insert: node }`, the node to insert there, either one undo step (or a promise of it; null does nothing, and so does a promise that settles after the document changed). `over` runs on every dragover, so it should be quick.
497
+
498
+ ```js
499
+ panels: [{
500
+ key: "files",
501
+ label: "Files",
502
+ render: (element, editor) => { /* rows whose dragstart does dataTransfer.setData("application/x-my-file+image", JSON.stringify(file)) */ },
503
+ drop: {
504
+ types: ["application/x-my-file+image", "application/x-my-file+document"],
505
+ over: (types, { node, editor }) => {
506
+ if (!types.includes("application/x-my-file+image")) return { insert: "button" }
507
+ return node.type === "image" ? { replace: node.id } : { insert: "image" }
508
+ },
509
+ onDrop: (dataTransfer, { node, replace, parentId, index, at, editor }) => {
510
+ const type = dataTransfer.types.find((each) => each.startsWith("application/x-my-file"))
511
+ const file = JSON.parse(dataTransfer.getData(type))
512
+ if (replace) return { values: { "attributes.src": file.url } }
513
+ return { insert: file.kind === "image" ? createNode("image", { attributes: { src: file.url } }) : createNode("button", { attributes: { href: file.url }, data: { content: file.name } }) }
514
+ }
515
+ }
516
+ }]
517
+ ```
518
+
519
+ `over` gets the block under the pointer (the root on an empty spot); `onDrop` gets the same, the `replace` id or the `parentId` and `index` it goes at (and `at`, `{ x, y }`, in a free layout). The demo's Note kit has a Files panel like this at `/?files=1`.
520
+
475
521
  Loading a document into the editor of another kit fails with a message saying which editor it is for.
476
522
 
477
523
  A kit can leave out `blocks`: with `blocks: false` it is its root alone, such as a text message with its fields in Inspect, and the editor has no Blocks and Layers panels. Its `canvas` and `outputs` hooks get the editor's `options` and `element` too, for a canvas that previews from your app (`/?kit=memo` in the demo), where a link with `data-tess-open` opens in a new tab instead of selecting; the canvas renders again when `data-tess-edit-variables-url-value` changes. A field's `hint` can be a function of its value, `` hint: (value, node) => `${value.length} / 160` ``, kept up to date as it's typed. Code fields color `html`, `xml`, `css` and `yaml`.
@@ -463,8 +463,9 @@ export default class extends Controller {
463
463
  build() {
464
464
  this.blocksElement = h("div", { class: "flex-1 overflow-y-auto pb-4" })
465
465
  this.treeElement = h("div", { class: "flex-1 overflow-y-auto" })
466
- // The kit's own panels (registerKit's panels), a tab each after Inspect
467
- this.kitPanels = (this.hasBlocks ? this.kit.panels || [] : []).map((panel) => ({ ...panel, element: h("div", { class: "hidden flex-1 overflow-y-auto min-h-0", dataset: { tessEditPanel: panel.key } }) }))
466
+ // The kit's own panels (registerKit's panels), a tab each after Inspect;
467
+ // those this editor has (visible), decided once
468
+ this.kitPanels = (this.hasBlocks ? this.kit.panels || [] : []).filter((panel) => !panel.visible || panel.visible(this)).map((panel) => ({ ...panel, element: h("div", { class: "hidden flex-1 overflow-y-auto min-h-0", dataset: { tessEditPanel: panel.key } }) }))
468
469
  this.inspectorElement = h("div", { class: "flex flex-1 flex-col min-h-0", dataset: { tessEditPanel: "inspect" } })
469
470
  // Each tab is a panel, in the pane on either side of the canvas it was put in
470
471
  this.tabs = this.hasBlocks
@@ -476,6 +477,9 @@ export default class extends Controller {
476
477
  ]
477
478
  : []
478
479
  this.arrangement = this.storedArrangement() || this.preset([this.storedPanels(), this.panelsValue].find((value) => PANELS.includes(value)) || "split")
480
+ // A kit's panel that asks to (open) is the tab open at first, in whichever pane shows it
481
+ const opened = this.kitPanels.find((panel) => panel.open?.(this))
482
+ if (opened) this.openTabs = { left: opened.key, right: opened.key, sheet: opened.key }
479
483
  this.panes = Object.fromEntries(SIDES.map((side) => [side, { side, element: h("aside", { dataset: { tessEditPane: side } }), strip: this.tabStrip(side) }]))
480
484
  this.inspectPanel = h("aside")
481
485
  this.sheetClose = h("button", { type: "button", title: t("Hide panels"), class: `${TOOL} ml-auto self-center`, html: icon("close"), onclick: () => this.setSheetOpen(false) })
@@ -577,16 +581,25 @@ export default class extends Controller {
577
581
  }
578
582
 
579
583
  // The tabs in the panes a preset puts them in: all on one side, or split
580
- // with Inspect on the right
584
+ // with Inspect on the right, and a kit's panel on its side (left unless
585
+ // its side is "right")
581
586
  preset(panels) {
582
587
  const keys = this.tabs.map((tab) => tab.key)
583
588
  if (panels === "left") return { left: keys, right: [] }
584
589
  if (panels === "right") return { left: [], right: keys }
585
- return { left: keys.filter((key) => key !== "inspect"), right: keys.filter((key) => key === "inspect") }
590
+ return { left: keys.filter((key) => this.splitSide(key) === "left"), right: keys.filter((key) => this.splitSide(key) === "right") }
591
+ }
592
+
593
+ // The side a tab is on when the panels are split
594
+ splitSide(key) {
595
+ if (key === "inspect") return "right"
596
+ return this.kitPanels.find((panel) => panel.key === key)?.side === "right" ? "right" : "left"
586
597
  }
587
598
 
588
599
  // Where the tabs were put last, with tabs it doesn't know of yet (a kit's
589
- // new panel) where the split preset puts them
600
+ // new panel, or one another editor of the kit doesn't have) where the split
601
+ // preset puts them, or in the other pane when that side has none: never a
602
+ // pane of its own
590
603
  storedArrangement() {
591
604
  if (!this.hasBlocks) return null
592
605
  let stored
@@ -599,10 +612,11 @@ export default class extends Controller {
599
612
  const keys = this.tabs.map((tab) => tab.key)
600
613
  const arrangement = Object.fromEntries(SIDES.map((side) => [side, [...new Set(stored[side])].filter((key) => keys.includes(key))]))
601
614
  arrangement.right = arrangement.right.filter((key) => !arrangement.left.includes(key))
602
- const split = this.preset("split")
603
615
  for (const key of keys) {
604
616
  if (arrangement.left.includes(key) || arrangement.right.includes(key)) continue
605
- arrangement[split.left.includes(key) ? "left" : "right"].push(key)
617
+ const side = this.splitSide(key)
618
+ const other = side === "left" ? "right" : "left"
619
+ arrangement[arrangement[side].length || !arrangement[other].length ? side : other].push(key)
606
620
  }
607
621
  return arrangement
608
622
  }
@@ -1293,6 +1307,51 @@ export default class extends Controller {
1293
1307
  }
1294
1308
  }
1295
1309
 
1310
+ // The drop hook of a kit's panel that takes what's dragged (by its
1311
+ // dataTransfer types), if one does (see core/registry.js, a panel's drop)
1312
+ dropHook(dataTransfer) {
1313
+ const types = [...(dataTransfer?.types || [])]
1314
+ return this.kitPanels.map((panel) => panel.drop).find((drop) => drop?.types?.some((type) => types.includes(type))) || null
1315
+ }
1316
+
1317
+ // Something from a kit's panel let go on the canvas at a target (ui/canvas.js's
1318
+ // panelTarget): the hook's onDrop says what it does, { values } set on the
1319
+ // block it replaces or { insert: node } put there, as one undo step. The
1320
+ // data is read while dropping; what onDrop returns may be a promise.
1321
+ async dropFromPanel(hook, dataTransfer, target) {
1322
+ const node = target.replace ? find(this.doc, target.replace)?.node : null
1323
+ const before = JSON.stringify(this.doc)
1324
+ let result
1325
+ try {
1326
+ result = await hook.onDrop(dataTransfer, { node, replace: target.replace || null, parentId: target.parentId ?? null, index: target.index ?? null, at: target.at || null, editor: this })
1327
+ } catch (error) {
1328
+ this.flash(error.message)
1329
+ return
1330
+ }
1331
+ // A promise that settles after the document changed doesn't apply: where
1332
+ // it was dropped may no longer be there
1333
+ if (!result || JSON.stringify(this.doc) !== before) return
1334
+ if (target.replace && result.values) {
1335
+ if (!find(this.doc, target.replace)) return
1336
+ this.place(target.replace, result.values)
1337
+ this.select(target.replace)
1338
+ } else if (!target.replace && result.insert) {
1339
+ this.insertNode(result.insert, { parentId: target.parentId, index: target.index, at: target.at })
1340
+ }
1341
+ }
1342
+
1343
+ // Puts a new block in a parent at an index (in a free layout at `at`, { x, y })
1344
+ // as one undo step and selects it; null when it can't go there (a lock, the
1345
+ // blocks allowed, or a block in it that doesn't fit)
1346
+ insertNode(node, { parentId = this.doc.id, index = null, at = null } = {}) {
1347
+ const parent = find(this.doc, parentId)?.node
1348
+ if (!parent || !node?.type || !this.canInsert(parentId, node.type) || misfit(node, accepted(this.doc, parentId, { moving: true }))) return null
1349
+ placed(node, at)
1350
+ this.commit((doc) => insert(doc, parentId, index ?? parent.children.length, node))
1351
+ this.select(node.id)
1352
+ return node
1353
+ }
1354
+
1296
1355
  // Sets several values of a block as one undo step, such as its x and y
1297
1356
  // when it is dragged in a free layout; a run of them on the same block
1298
1357
  // (arrow-key nudges) as one, with `coalesce`
@@ -92,13 +92,33 @@
92
92
  // // message, has no Blocks and Layers panels
93
93
  // panels: [{ key: "test", label: "Test", render: (element, editor) => {} }],
94
94
  // // optional: panels of the kit's own, a tab each after
95
- // // Layers (and Inspect, when it is a tab). render fills its
96
- // // element once, when the editor is built; editor is the
95
+ // // Layers (and Inspect, when it is a tab), each with,
96
+ // // optionally, visible: (editor) => false to leave it out
97
+ // // of an editor (decided once, when it's built, e.g. by
98
+ // // editor.slot(name)), side: "right" to be beside Inspect
99
+ // // when the panels are split (else left, with Layers), and
100
+ // // open: (editor) => true to be the tab open at first.
101
+ // // A stored arrangement without it puts it there too,
102
+ // // or in the other pane when that side has none.
103
+ // // render fills its element once, when the editor is
104
+ // // built; editor is the
97
105
  // // editor (its doc, select, renderCanvas, and its element,
98
106
  // // which sends tess-edit:change). The canvas is told which
99
107
  // // one is open (panel) and drawn again when that changes;
100
108
  // // a panel that changes what it shows calls
101
109
  // // editor.renderCanvas(). label is translated (t)
110
+ // // drop: { types, over, onDrop } takes what is dragged from the
111
+ // // panel onto the canvas, by its dataTransfer types:
112
+ // // drop: { types: ["application/x-my-file+image"],
113
+ // // over: (types, { node, editor }) => ({ replace: node.id }) or ({ insert: "image" }) or null,
114
+ // // onDrop: (dataTransfer, { node, replace, parentId, index, at, editor }) =>
115
+ // // ({ values: { "attributes.src": url } }) or ({ insert: createNode("image", …) }) or null }
116
+ // // over says while it's dragged (when only the types can be read)
117
+ // // what letting go there would do: change the block it's over
118
+ // // (replace, an id), or insert a block of a type where a Blocks
119
+ // // tile would go, with the same indicator. onDrop reads the data
120
+ // // and gives the change (or a promise of it): values set on the
121
+ // // block it replaces, or the node to insert; one undo step either way
102
122
  // notes: { html: "…" }, // optional: what each output is, in the Code dialog
103
123
  // errors: "The note could not be rendered" // shown before the reasons
104
124
  // })
@@ -10,10 +10,10 @@ export { EditorController }
10
10
  export { COMPILE_URL, compile } from "tess_edit/export/compile"
11
11
  export { escapeAttribute, escapeText, richTextToHtml } from "tess_edit/export/richtext"
12
12
  export { tableHtml } from "tess_edit/export/table"
13
- export { contentNode, createDocument, createNode, findType, normalize, renumber, withContent } from "tess_edit/core/document"
13
+ export { contentNode, createDocument, createNode, find, findType, getValue, normalize, renumber, withContent } from "tess_edit/core/document"
14
14
  export { baseBreakpoint, breakpointsOf, hasOverride, inheritedAt, resetAt, setValueAt, valueAt } from "tess_edit/core/responsive"
15
15
  export { CONTENT_END, CONTENT_START } from "tess_edit/blocks/content"
16
- export { registerBlock, registerCategory, registerFieldType, registerKit, registerPaletteSource, extendBlock, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
16
+ export { registerBlock, registerCategory, registerFieldType, registerKit, registerPaletteSource, extendBlock, fieldsFor, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
17
17
  export * as fields from "tess_edit/blocks/fields"
18
18
  export { imageField } from "tess_edit/core/media"
19
19
  export { cellOf, tableOf } from "tess_edit/core/table"
@@ -587,6 +587,8 @@ export class Canvas {
587
587
 
588
588
  onDragOver(event) {
589
589
  if (isFileDrag(event, this.editor)) return this.onFileDragOver(event)
590
+ const hook = !this.editor.drag && this.editor.dropHook(event.dataTransfer)
591
+ if (hook) return this.onPanelDragOver(event, hook)
590
592
  const drag = this.editor.drag
591
593
  if (!drag) return
592
594
  const target = this.free.dropTarget(event.clientX, event.clientY, drag) || this.dropTarget(event.clientX, event.clientY, drag)
@@ -601,6 +603,8 @@ export class Canvas {
601
603
 
602
604
  onDrop(event) {
603
605
  if (isFileDrag(event, this.editor)) return this.onFileDrop(event)
606
+ const hook = !this.editor.drag && this.editor.dropHook(event.dataTransfer)
607
+ if (hook) return this.onPanelDrop(event, hook)
604
608
  const drag = this.editor.drag
605
609
  if (!drag) return
606
610
  event.preventDefault()
@@ -610,6 +614,39 @@ export class Canvas {
610
614
  this.editor.endDrag()
611
615
  }
612
616
 
617
+ // Something dragged from a kit's panel that its drop hook takes (see
618
+ // core/registry.js, a panel's drop): where its over() says it would go,
619
+ // shown as a Blocks drag is, the block it replaces filled
620
+ panelTarget(event, hook) {
621
+ const doc = this.editor.doc
622
+ const hit = this.hit(event.target)
623
+ const node = (hit && !hit.placeholder && find(doc, hit.id)?.node) || doc
624
+ const answer = hook.over?.([...(event.dataTransfer?.types || [])], { node, editor: this.editor })
625
+ if (answer?.replace && find(doc, answer.replace)) {
626
+ const rect = this.rectOf(answer.replace)
627
+ return rect ? { replace: answer.replace, fill: rect } : null
628
+ }
629
+ if (!answer?.insert) return null
630
+ const drag = { kind: "new", type: answer.insert }
631
+ return this.free.dropTarget(event.clientX, event.clientY, drag) || this.dropTarget(event.clientX, event.clientY, drag)
632
+ }
633
+
634
+ onPanelDragOver(event, hook) {
635
+ const target = this.panelTarget(event, hook)
636
+ if (!target) return this.hideIndicator()
637
+ event.preventDefault()
638
+ event.dataTransfer.dropEffect = "copy"
639
+ this.showIndicator(target)
640
+ }
641
+
642
+ onPanelDrop(event, hook) {
643
+ const target = this.panelTarget(event, hook)
644
+ this.hideIndicator()
645
+ if (!target) return
646
+ event.preventDefault()
647
+ this.editor.dropFromPanel(hook, event.dataTransfer, target)
648
+ }
649
+
613
650
  // An image file dragged from the desktop onto a block with an image field
614
651
  // uploads it there: an Image block's image, or the background of the
615
652
  // section (or other block with an image field) the pointer is in. Elsewhere the drop is refused, so
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tess_edit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt