tess_edit 0.4.0 → 0.4.2
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 +4 -4
- data/README.md +2 -0
- data/app/javascript/tess_edit/controllers/editor_controller.js +44 -8
- data/app/javascript/tess_edit/core/registry.js +4 -0
- data/app/javascript/tess_edit/ui/canvas.js +14 -6
- data/app/javascript/tess_edit/ui/free_layout.js +267 -0
- data/lib/tess_edit/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b135e56fcad4ed593a2126a6196f218007859254ab97999b65864e5600764335
|
|
4
|
+
data.tar.gz: 04cad6e0e9b1bd1b96043dee46715db7d4dcccfee69ab9c0922735b225341aa5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa237f8f3887618415fefe2c27a1a507eb2f112f30aad0ed1a4c62be60b50edd0b0320367a98890a3a228f66cbcc8e1df2a5e8596854e5a0a9114ef7d88a84a2
|
|
7
|
+
data.tar.gz: 2d45642436414759612252f86b67ccde984118d5f2fe16cb0de01e8ab9cc82ed30e508f81adb5951492e983a527eae9d7a2a0c405425908145d45d8791b133b1
|
data/README.md
CHANGED
|
@@ -89,6 +89,8 @@ The editor loads the document from the `jsonInput` (or from `data-tess-edit-docu
|
|
|
89
89
|
|
|
90
90
|
`data-tess-edit-panels-value` sets the default panel layout: `split` (Blocks and Layers on the left, Inspect on the right, the default), `left` or `right` (Blocks, Layers and Inspect as tabs in one sidebar on that side). People can switch with the buttons in the top bar; their choice is remembered in the browser. When the editor is narrower than 1000px, it puts all panels on the left so the canvas keeps its room, and goes back to the chosen layout when it is wide enough again.
|
|
91
91
|
|
|
92
|
+
The editor has a border with rounded corners. A page that frames it itself, such as a card it fills, turns it off with `data-tess-edit-frame-value="false"`.
|
|
93
|
+
|
|
92
94
|
Every change updates the inputs and dispatches `tess-edit:change` with `{ json, mjml, html, errors, document }` in `event.detail`. When the MJML can't be compiled, `html` is empty and `errors` says why; the editor shows the reason under the canvas. The controller instance is available as `element.tessEdit`, with `getDocument()`, `setDocument(json)` and `export()`.
|
|
93
95
|
|
|
94
96
|
### What gets stored
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// registered (see core/registry.js)
|
|
6
6
|
// document JSON of the document to load (falls back to the json input, then a sample)
|
|
7
7
|
// sample load the sample document when there is nothing else (default true)
|
|
8
|
+
// frame a border with rounded corners around the editor (default true); a
|
|
9
|
+
// host that frames it itself, such as a card it fills, turns it off
|
|
8
10
|
// panels "split" (Blocks and Layers left, Inspect right), "left" or "right" (all panels
|
|
9
11
|
// as tabs on one side). The default; a choice made in the editor's
|
|
10
12
|
// top bar is remembered per browser. Below NARROW_WIDTH the editor
|
|
@@ -47,6 +49,7 @@ import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
|
47
49
|
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
48
50
|
import { Variables, loopScope } from "tess_edit/core/variables"
|
|
49
51
|
import { Canvas } from "tess_edit/ui/canvas"
|
|
52
|
+
import { freeChild, number } from "tess_edit/ui/free_layout"
|
|
50
53
|
import { Tree } from "tess_edit/ui/tree"
|
|
51
54
|
import { Inspector } from "tess_edit/ui/inspector"
|
|
52
55
|
import { TextEditing } from "tess_edit/ui/text_editing"
|
|
@@ -64,9 +67,16 @@ const PANELS_KEY = "tess_edit:panels"
|
|
|
64
67
|
const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
|
|
65
68
|
// Two sidebars (256px + 320px) leave too little canvas below this editor width.
|
|
66
69
|
const NARROW_WIDTH = 1000
|
|
70
|
+
const ARROWS = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
|
|
71
|
+
|
|
72
|
+
// A new block where it was let go in a free layout
|
|
73
|
+
function placed(node, at) {
|
|
74
|
+
if (at) node.data = { ...node.data, x: at.x, y: at.y }
|
|
75
|
+
return node
|
|
76
|
+
}
|
|
67
77
|
|
|
68
78
|
export default class extends Controller {
|
|
69
|
-
static values = { kit: { type: String, default: DEFAULT_KIT }, document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, translationsUrl: String, lock: String, blocks: String, options: String }
|
|
79
|
+
static values = { kit: { type: String, default: DEFAULT_KIT }, document: String, sample: { type: Boolean, default: true }, frame: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, translationsUrl: String, lock: String, blocks: String, options: String }
|
|
70
80
|
static targets = ["jsonInput", "mjmlInput", "htmlInput"]
|
|
71
81
|
|
|
72
82
|
connect() {
|
|
@@ -357,7 +367,7 @@ export default class extends Controller {
|
|
|
357
367
|
|
|
358
368
|
this.root = h(
|
|
359
369
|
"div",
|
|
360
|
-
{ class:
|
|
370
|
+
{ class: `tess-edit relative flex flex-col h-full min-h-[560px] overflow-hidden ${this.frameValue ? "rounded-lg border border-gray-200 dark:border-gray-700 " : ""}bg-gray-50 dark:bg-gray-900 text-sm text-gray-800 dark:text-gray-200 antialiased dark:[color-scheme:dark]`, tabindex: "-1", onkeydown: (event) => this.onKeydown(event) },
|
|
361
371
|
this.topBar,
|
|
362
372
|
this.body
|
|
363
373
|
)
|
|
@@ -730,20 +740,43 @@ export default class extends Controller {
|
|
|
730
740
|
this.tree.clearMarker()
|
|
731
741
|
}
|
|
732
742
|
|
|
733
|
-
|
|
743
|
+
// `at` is where in a free layout (ui/free_layout.js) it was let go: { x, y }
|
|
744
|
+
drop(drag, parentId, index, at = null) {
|
|
734
745
|
if (!drag || !this.canFill(parentId) || (drag.kind === "move" && !this.isFree(drag.id))) return
|
|
735
746
|
if (drag.kind === "source") {
|
|
736
|
-
this.insertFromSource(drag.source, drag.item, { parentId, index })
|
|
747
|
+
this.insertFromSource(drag.source, drag.item, { parentId, index, at })
|
|
737
748
|
} else if (drag.kind === "new") {
|
|
738
|
-
const node = createFromPalette(drag.key)
|
|
749
|
+
const node = placed(createFromPalette(drag.key), at)
|
|
739
750
|
this.commit((doc) => insert(doc, parentId, index, node))
|
|
740
751
|
this.select(node.id)
|
|
741
752
|
} else {
|
|
742
|
-
this.commit((doc) =>
|
|
753
|
+
this.commit((doc) => {
|
|
754
|
+
move(doc, drag.id, parentId, index)
|
|
755
|
+
if (at) placed(find(doc, drag.id).node, at)
|
|
756
|
+
})
|
|
743
757
|
this.select(drag.id)
|
|
744
758
|
}
|
|
745
759
|
}
|
|
746
760
|
|
|
761
|
+
// Sets several values of a block as one undo step, such as its x and y
|
|
762
|
+
// when it is dragged in a free layout; a run of them on the same block
|
|
763
|
+
// (arrow-key nudges) as one, with `coalesce`
|
|
764
|
+
place(id, values, { coalesce = null } = {}) {
|
|
765
|
+
const editable = Object.entries(values).filter(([key, value]) => key && key !== "undefined" && value !== undefined && this.canEditField(id, key))
|
|
766
|
+
if (!editable.length) return
|
|
767
|
+
if (this.editingId) this.stopEditing()
|
|
768
|
+
this.commit((doc) => editable.forEach(([key, value]) => setValue(doc, id, key, value)), { coalesce })
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Moves the selection in a free layout by whole units
|
|
772
|
+
nudge(dx, dy) {
|
|
773
|
+
const child = freeChild(this.doc, this.selectedId)
|
|
774
|
+
if (!child || !this.isFree(child.node.id)) return false
|
|
775
|
+
const { node } = child
|
|
776
|
+
this.place(node.id, { "data.x": Math.max(0, number(node.data?.x) + dx), "data.y": Math.max(0, number(node.data?.y) + dy) }, { coalesce: `${node.id}:nudge` })
|
|
777
|
+
return true
|
|
778
|
+
}
|
|
779
|
+
|
|
747
780
|
insertAt(key, parentId, index) {
|
|
748
781
|
if (!this.canFill(parentId)) return
|
|
749
782
|
const node = createFromPalette(key)
|
|
@@ -785,10 +818,10 @@ export default class extends Controller {
|
|
|
785
818
|
|
|
786
819
|
// Inserts what a palette source's tile creates (see core/registry.js) at a
|
|
787
820
|
// place, or where a click on a tile would add it
|
|
788
|
-
async insertFromSource(source, item, { parentId, index } = {}) {
|
|
821
|
+
async insertFromSource(source, item, { parentId, index, at = null } = {}) {
|
|
789
822
|
let node
|
|
790
823
|
try {
|
|
791
|
-
node = await source.create(item, this)
|
|
824
|
+
node = placed(await source.create(item, this), at)
|
|
792
825
|
} catch (error) {
|
|
793
826
|
this.flash(error.message)
|
|
794
827
|
return null
|
|
@@ -902,6 +935,9 @@ export default class extends Controller {
|
|
|
902
935
|
} else if (event.key === "Escape") {
|
|
903
936
|
this.closeMenu()
|
|
904
937
|
this.selectParent()
|
|
938
|
+
} else if (ARROWS[event.key]) {
|
|
939
|
+
const [dx, dy] = ARROWS[event.key].map((step) => step * (event.shiftKey ? 10 : 1))
|
|
940
|
+
if (this.nudge(dx, dy)) event.preventDefault()
|
|
905
941
|
}
|
|
906
942
|
}
|
|
907
943
|
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
// palette: [{ key, label, icon, category, create: ({ createNode }) => node }], // optional
|
|
20
20
|
// kits: ["email"], // the kits it is part of, default ["email"]
|
|
21
21
|
// unique: true, // optional: at most one per document, like Content
|
|
22
|
+
// layout: "free", // optional, on a container: its children are placed by
|
|
23
|
+
// // data.x and data.y, dragged on the canvas (ui/free_layout.js)
|
|
24
|
+
// resize: { width: "data.width", height: "data.height" },
|
|
25
|
+
// // optional, in a free layout: the keys of its size, for a resize handle
|
|
22
26
|
// render: { mjml: (node, ctx) => "<mj-text …>" } // one per format of its kits:
|
|
23
27
|
// // mjml (export/mjml.js), html (export/html.js)
|
|
24
28
|
// })
|
|
@@ -8,6 +8,7 @@ import { ancestors, find, isDescendant, isHorizontal } from "tess_edit/core/docu
|
|
|
8
8
|
import { imageFields } from "tess_edit/core/media"
|
|
9
9
|
import { getBlock, label } from "tess_edit/core/registry"
|
|
10
10
|
import { missingArguments, textParts, translationCalls } from "tess_edit/core/translations"
|
|
11
|
+
import { FREE_CSS, FreeLayout } from "tess_edit/ui/free_layout"
|
|
11
12
|
import { icon } from "tess_edit/ui/icons"
|
|
12
13
|
|
|
13
14
|
const OVERLAY_CSS = `
|
|
@@ -58,6 +59,7 @@ export class Canvas {
|
|
|
58
59
|
this.frame = frame
|
|
59
60
|
this.hoverId = null
|
|
60
61
|
this.mode = "edit"
|
|
62
|
+
this.free = new FreeLayout(this)
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
get doc() {
|
|
@@ -77,7 +79,7 @@ export class Canvas {
|
|
|
77
79
|
doc.write(html)
|
|
78
80
|
doc.close()
|
|
79
81
|
const style = doc.createElement("style")
|
|
80
|
-
style.textContent = OVERLAY_CSS
|
|
82
|
+
style.textContent = OVERLAY_CSS + FREE_CSS
|
|
81
83
|
doc.head.append(style)
|
|
82
84
|
this.overlay = doc.createElement("div")
|
|
83
85
|
this.overlay.id = "tess-overlay"
|
|
@@ -96,6 +98,7 @@ export class Canvas {
|
|
|
96
98
|
const doc = this.doc
|
|
97
99
|
doc.addEventListener("mousemove", (event) => this.onMove(event))
|
|
98
100
|
doc.addEventListener("mouseleave", () => this.setHover(null))
|
|
101
|
+
doc.addEventListener("pointerdown", (event) => this.free.onPointerDown(event))
|
|
99
102
|
doc.addEventListener("click", (event) => this.onClick(event), true)
|
|
100
103
|
doc.addEventListener("dblclick", (event) => this.onDoubleClick(event))
|
|
101
104
|
doc.addEventListener("dragover", (event) => this.onDragOver(event))
|
|
@@ -230,6 +233,7 @@ export class Canvas {
|
|
|
230
233
|
if (!this.overlay) return
|
|
231
234
|
this.overlay.replaceChildren()
|
|
232
235
|
if (this.mode !== "edit") return
|
|
236
|
+
this.free.drawGuides(this.overlay)
|
|
233
237
|
const { selectedId, editingId } = this.editor
|
|
234
238
|
|
|
235
239
|
if (this.hoverId && this.hoverId !== selectedId) {
|
|
@@ -250,7 +254,10 @@ export class Canvas {
|
|
|
250
254
|
const free = this.editor.isFree(selectedId)
|
|
251
255
|
box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: editingId === selectedId ? "Editing text · Esc to finish" : free || !this.editor.lock ? label(hit.node) : `${label(hit.node)} · Layout` }))
|
|
252
256
|
|
|
253
|
-
|
|
257
|
+
const handle = this.free.handle(selectedId)
|
|
258
|
+
if (handle) box.append(handle)
|
|
259
|
+
|
|
260
|
+
if (editingId !== selectedId && !this.free.dragging) {
|
|
254
261
|
const tools = this.doc.createElement("div")
|
|
255
262
|
tools.className = "tess-tools"
|
|
256
263
|
tools.innerHTML = [
|
|
@@ -311,7 +318,7 @@ export class Canvas {
|
|
|
311
318
|
// --- events ---------------------------------------------------------------
|
|
312
319
|
|
|
313
320
|
onMove(event) {
|
|
314
|
-
if (this.editor.drag) return
|
|
321
|
+
if (this.editor.drag || this.free.dragging) return
|
|
315
322
|
const hit = this.hit(event.target)
|
|
316
323
|
this.setHover(hit && !hit.placeholder ? hit.id : null)
|
|
317
324
|
}
|
|
@@ -320,6 +327,7 @@ export class Canvas {
|
|
|
320
327
|
if (this.editor.editingId && event.target.closest?.(".ProseMirror")) return
|
|
321
328
|
if (event.target.closest?.("#tess-overlay")) return
|
|
322
329
|
event.preventDefault()
|
|
330
|
+
if (this.free.swallowClick()) return
|
|
323
331
|
const hit = this.hit(event.target)
|
|
324
332
|
if (hit?.placeholder) {
|
|
325
333
|
const rect = this.toParentRect(hit.element.getBoundingClientRect())
|
|
@@ -343,7 +351,7 @@ export class Canvas {
|
|
|
343
351
|
if (isFileDrag(event, this.editor)) return this.onFileDragOver(event)
|
|
344
352
|
const drag = this.editor.drag
|
|
345
353
|
if (!drag) return
|
|
346
|
-
const target = this.dropTarget(event.clientX, event.clientY, drag)
|
|
354
|
+
const target = this.free.dropTarget(event.clientX, event.clientY, drag) || this.dropTarget(event.clientX, event.clientY, drag)
|
|
347
355
|
if (!target) {
|
|
348
356
|
this.hideIndicator()
|
|
349
357
|
return
|
|
@@ -358,9 +366,9 @@ export class Canvas {
|
|
|
358
366
|
const drag = this.editor.drag
|
|
359
367
|
if (!drag) return
|
|
360
368
|
event.preventDefault()
|
|
361
|
-
const target = this.dropTarget(event.clientX, event.clientY, drag)
|
|
369
|
+
const target = this.free.dropTarget(event.clientX, event.clientY, drag) || this.dropTarget(event.clientX, event.clientY, drag)
|
|
362
370
|
this.hideIndicator()
|
|
363
|
-
if (target) this.editor.drop(drag, target.parentId, target.index)
|
|
371
|
+
if (target) this.editor.drop(drag, target.parentId, target.index, target.at)
|
|
364
372
|
this.editor.endDrag()
|
|
365
373
|
}
|
|
366
374
|
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// Free layout: a container whose definition has layout: "free" places its
|
|
2
|
+
// children by x and y (data.x, data.y, in the kit's own units, such as a
|
|
3
|
+
// label's dots) instead of one after another. On the canvas its children
|
|
4
|
+
// are dragged to move them, nudged with the arrow keys (Shift: by 10), and
|
|
5
|
+
// resized by a handle when their definition says what their size is:
|
|
6
|
+
//
|
|
7
|
+
// registerBlock({ type: "label", accepts: ["field"], layout: "free", … })
|
|
8
|
+
// registerBlock({ type: "box", roles: ["field"], resize: { width: "data.width", height: "data.height" }, … })
|
|
9
|
+
//
|
|
10
|
+
// The container's element says how many units wide it is
|
|
11
|
+
// (data-tess-width="812"), so pointer moves become units; without it a
|
|
12
|
+
// unit is one of its CSS pixels. Edges snap to the container's and the
|
|
13
|
+
// other children's edges and middles, with a guide; Alt turns that off.
|
|
14
|
+
// A block dropped from the palette lands where it is let go.
|
|
15
|
+
|
|
16
|
+
import { find, getValue } from "tess_edit/core/document"
|
|
17
|
+
import { getBlock } from "tess_edit/core/registry"
|
|
18
|
+
|
|
19
|
+
// Screen pixels a press moves before it drags, and within which edges snap
|
|
20
|
+
const THRESHOLD = 3
|
|
21
|
+
const SNAP = 5
|
|
22
|
+
|
|
23
|
+
export const FREE_CSS = `
|
|
24
|
+
.tess-guide{position:absolute;background:#ec4899;pointer-events:none}
|
|
25
|
+
.tess-resize{position:absolute;right:-5px;bottom:-5px;width:10px;height:10px;box-sizing:border-box;background:#fff;border:2px solid #2563eb;border-radius:2px;pointer-events:auto;cursor:nwse-resize;touch-action:none}
|
|
26
|
+
.tess-free-dragging,.tess-free-dragging *{cursor:grabbing!important;user-select:none}
|
|
27
|
+
`
|
|
28
|
+
|
|
29
|
+
export function isFreeLayout(node) {
|
|
30
|
+
return getBlock(node?.type)?.layout === "free"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The block in a free container that holds the node, from the node outwards
|
|
34
|
+
export function freeChild(doc, id) {
|
|
35
|
+
let hit = find(doc, id)
|
|
36
|
+
while (hit?.parent) {
|
|
37
|
+
if (isFreeLayout(hit.parent)) return hit
|
|
38
|
+
hit = find(doc, hit.parent.id)
|
|
39
|
+
}
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const number = (value) => {
|
|
44
|
+
const result = Number(value)
|
|
45
|
+
return Number.isFinite(result) ? result : 0
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The nearest of the candidates to one of the edges, within SNAP: the
|
|
49
|
+
// offset that puts it there and where the guide goes
|
|
50
|
+
export function snap(edges, candidates) {
|
|
51
|
+
let best = null
|
|
52
|
+
for (const edge of edges) {
|
|
53
|
+
for (const candidate of candidates) {
|
|
54
|
+
const offset = candidate - edge
|
|
55
|
+
if (Math.abs(offset) <= SNAP && (!best || Math.abs(offset) < Math.abs(best.offset))) best = { offset, at: candidate }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return best
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class FreeLayout {
|
|
62
|
+
constructor(canvas) {
|
|
63
|
+
this.canvas = canvas
|
|
64
|
+
this.gesture = null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get editor() {
|
|
68
|
+
return this.canvas.editor
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get dragging() {
|
|
72
|
+
return !!this.gesture?.active
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
container(parentId) {
|
|
76
|
+
return this.canvas.elementOf(parentId) || (parentId === this.editor.doc.id ? this.canvas.doc.body : null)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Screen pixels per unit of a free container, and CSS pixels per screen
|
|
80
|
+
// pixel inside it (the canvas may be zoomed)
|
|
81
|
+
scale(parentId) {
|
|
82
|
+
const element = this.container(parentId)
|
|
83
|
+
const rect = element?.getBoundingClientRect()
|
|
84
|
+
if (!rect?.width) return { unit: 1, css: 1, rect }
|
|
85
|
+
const css = element.offsetWidth ? element.offsetWidth / rect.width : 1
|
|
86
|
+
const units = Number(element.dataset.tessWidth)
|
|
87
|
+
return { unit: units > 0 ? rect.width / units : 1 / css, css, rect }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// --- moving and resizing -------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
onPointerDown(event) {
|
|
93
|
+
if (event.button !== 0 || this.editor.editingId || this.canvas.mode !== "edit") return
|
|
94
|
+
if (event.target.closest?.("#tess-overlay")) return
|
|
95
|
+
const hit = this.canvas.hit(event.target)
|
|
96
|
+
if (!hit || hit.placeholder) return
|
|
97
|
+
const child = freeChild(this.editor.doc, hit.id)
|
|
98
|
+
if (child && this.editor.isFree(child.node.id)) this.start(event, "move", child)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The selected block's resize handle, when its definition says what its size is
|
|
102
|
+
handle(id) {
|
|
103
|
+
const child = freeChild(this.editor.doc, id)
|
|
104
|
+
const resize = child && getBlock(child.node.type)?.resize
|
|
105
|
+
if (!resize || !this.editor.isFree(id) || this.editor.editingId) return null
|
|
106
|
+
const handle = this.canvas.doc.createElement("div")
|
|
107
|
+
handle.className = "tess-resize"
|
|
108
|
+
handle.title = "Drag to resize"
|
|
109
|
+
handle.addEventListener("pointerdown", (event) => {
|
|
110
|
+
event.preventDefault()
|
|
111
|
+
event.stopPropagation()
|
|
112
|
+
this.start(event, "resize", child)
|
|
113
|
+
})
|
|
114
|
+
return handle
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
start(event, kind, child) {
|
|
118
|
+
const { node, parent } = child
|
|
119
|
+
const element = this.canvas.elementOf(node.id)
|
|
120
|
+
if (!element) return
|
|
121
|
+
const resize = getBlock(node.type)?.resize || {}
|
|
122
|
+
const others = parent.children.filter((other) => other.id !== node.id).map((other) => this.canvas.rectOf(other.id)).filter(Boolean)
|
|
123
|
+
const { unit, css, rect: frame } = this.scale(parent.id)
|
|
124
|
+
this.gesture = {
|
|
125
|
+
kind,
|
|
126
|
+
id: node.id,
|
|
127
|
+
element,
|
|
128
|
+
resize,
|
|
129
|
+
unit,
|
|
130
|
+
css,
|
|
131
|
+
x: event.clientX,
|
|
132
|
+
y: event.clientY,
|
|
133
|
+
rect: element.getBoundingClientRect(),
|
|
134
|
+
size: { width: element.style.width, height: element.style.height },
|
|
135
|
+
values: { x: number(node.data?.x), y: number(node.data?.y), width: resize.width ? number(getValue(node, resize.width)) : 0, height: resize.height ? number(getValue(node, resize.height)) : 0 },
|
|
136
|
+
targets: {
|
|
137
|
+
x: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.left, rect.left + rect.width / 2, rect.right]),
|
|
138
|
+
y: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.top, rect.top + rect.height / 2, rect.bottom])
|
|
139
|
+
},
|
|
140
|
+
frame,
|
|
141
|
+
pointerId: event.pointerId,
|
|
142
|
+
target: event.target
|
|
143
|
+
}
|
|
144
|
+
const doc = this.canvas.doc
|
|
145
|
+
this.onMove = (moveEvent) => this.move(moveEvent)
|
|
146
|
+
this.onUp = (upEvent) => this.end(upEvent)
|
|
147
|
+
doc.addEventListener("pointermove", this.onMove)
|
|
148
|
+
doc.addEventListener("pointerup", this.onUp)
|
|
149
|
+
doc.addEventListener("pointercancel", this.onUp)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
move(event) {
|
|
153
|
+
const gesture = this.gesture
|
|
154
|
+
if (!gesture || event.pointerId !== gesture.pointerId) return
|
|
155
|
+
let dx = event.clientX - gesture.x
|
|
156
|
+
let dy = event.clientY - gesture.y
|
|
157
|
+
if (!gesture.active) {
|
|
158
|
+
if (Math.hypot(dx, dy) < THRESHOLD) return
|
|
159
|
+
gesture.active = true
|
|
160
|
+
try {
|
|
161
|
+
gesture.target.setPointerCapture?.(gesture.pointerId)
|
|
162
|
+
} catch {}
|
|
163
|
+
this.canvas.doc.documentElement.classList.add("tess-free-dragging")
|
|
164
|
+
this.editor.closeMenu()
|
|
165
|
+
this.editor.select(gesture.id)
|
|
166
|
+
this.canvas.setHover(null)
|
|
167
|
+
}
|
|
168
|
+
event.preventDefault()
|
|
169
|
+
const { rect } = gesture
|
|
170
|
+
const guides = []
|
|
171
|
+
if (!event.altKey) {
|
|
172
|
+
const xs = gesture.kind === "move" ? [rect.left + dx, rect.left + rect.width / 2 + dx, rect.right + dx] : [rect.right + dx]
|
|
173
|
+
const ys = gesture.kind === "move" ? [rect.top + dy, rect.top + rect.height / 2 + dy, rect.bottom + dy] : [rect.bottom + dy]
|
|
174
|
+
const x = snap(xs, gesture.targets.x)
|
|
175
|
+
const y = snap(ys, gesture.targets.y)
|
|
176
|
+
if (x) {
|
|
177
|
+
dx += x.offset
|
|
178
|
+
guides.push({ left: x.at, top: gesture.frame.top, width: 1, height: gesture.frame.height })
|
|
179
|
+
}
|
|
180
|
+
if (y) {
|
|
181
|
+
dy += y.offset
|
|
182
|
+
guides.push({ left: gesture.frame.left, top: y.at, width: gesture.frame.width, height: 1 })
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Whole units, shown as they will be
|
|
186
|
+
const du = Math.round(dx / gesture.unit)
|
|
187
|
+
const dv = Math.round(dy / gesture.unit)
|
|
188
|
+
const px = (units) => `${units * gesture.unit * gesture.css}px`
|
|
189
|
+
if (gesture.kind === "move") {
|
|
190
|
+
gesture.change = { x: gesture.values.x + du, y: gesture.values.y + dv }
|
|
191
|
+
gesture.element.style.translate = `${px(du)} ${px(dv)}`
|
|
192
|
+
} else {
|
|
193
|
+
gesture.change = this.resized(gesture, du, dv)
|
|
194
|
+
const width = gesture.resize.width ? gesture.change.width - gesture.values.width : 0
|
|
195
|
+
const height = gesture.resize.height ? gesture.change.height - gesture.values.height : 0
|
|
196
|
+
gesture.element.style.width = `calc(${px(rect.width / gesture.unit)} + ${px(width)})`
|
|
197
|
+
gesture.element.style.height = `calc(${px(rect.height / gesture.unit)} + ${px(height)})`
|
|
198
|
+
}
|
|
199
|
+
this.guides = guides
|
|
200
|
+
this.canvas.refresh()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// The new size; one key for both (a circle's diameter) takes the larger change
|
|
204
|
+
resized(gesture, du, dv) {
|
|
205
|
+
const { width, height } = gesture.values
|
|
206
|
+
if (gesture.resize.width && gesture.resize.width === gesture.resize.height) {
|
|
207
|
+
const size = Math.max(1, width + (Math.abs(du) > Math.abs(dv) ? du : dv))
|
|
208
|
+
return { width: size, height: size }
|
|
209
|
+
}
|
|
210
|
+
return { width: Math.max(1, width + du), height: Math.max(1, height + dv) }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
end(event) {
|
|
214
|
+
const gesture = this.gesture
|
|
215
|
+
if (!gesture || event.pointerId !== gesture.pointerId) return
|
|
216
|
+
const doc = this.canvas.doc
|
|
217
|
+
doc.removeEventListener("pointermove", this.onMove)
|
|
218
|
+
doc.removeEventListener("pointerup", this.onUp)
|
|
219
|
+
doc.removeEventListener("pointercancel", this.onUp)
|
|
220
|
+
doc.documentElement.classList.remove("tess-free-dragging")
|
|
221
|
+
this.gesture = null
|
|
222
|
+
this.guides = []
|
|
223
|
+
if (!gesture.active) return
|
|
224
|
+
// The click that ends a drag isn't one (see swallowClick)
|
|
225
|
+
this.dragged = true
|
|
226
|
+
setTimeout(() => (this.dragged = false))
|
|
227
|
+
gesture.element.style.translate = ""
|
|
228
|
+
Object.assign(gesture.element.style, gesture.size)
|
|
229
|
+
const change = gesture.change || {}
|
|
230
|
+
if (event.type === "pointercancel") return this.canvas.refresh()
|
|
231
|
+
if (gesture.kind === "move") this.editor.place(gesture.id, { "data.x": change.x, "data.y": change.y })
|
|
232
|
+
else this.editor.place(gesture.id, { [gesture.resize.width]: change.width, [gesture.resize.height]: change.height })
|
|
233
|
+
this.canvas.refresh()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Whether a click ends a drag, and so selects or edits nothing
|
|
237
|
+
swallowClick() {
|
|
238
|
+
const dragged = this.dragged
|
|
239
|
+
this.dragged = false
|
|
240
|
+
return dragged
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// --- the overlay -----------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
drawGuides(overlay) {
|
|
246
|
+
for (const rect of this.guides || []) overlay.append(this.canvas.box(rect, "tess-guide"))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// --- dropping from the palette -------------------------------------------------
|
|
250
|
+
|
|
251
|
+
// A free container under the pointer that takes the dragged block, and
|
|
252
|
+
// where in it, in units
|
|
253
|
+
dropTarget(x, y, drag) {
|
|
254
|
+
const doc = this.editor.doc
|
|
255
|
+
const hit = this.canvas.hit(this.canvas.doc.elementFromPoint(x, y))
|
|
256
|
+
let current = find(doc, hit ? hit.id : doc.id)
|
|
257
|
+
while (current && !isFreeLayout(current.node)) current = current.parent && find(doc, current.parent.id)
|
|
258
|
+
if (!current || !this.canvas.allowed(current.node.id, drag)) return null
|
|
259
|
+
const container = current.node
|
|
260
|
+
const { unit, rect } = this.scale(container.id)
|
|
261
|
+
if (!rect) return null
|
|
262
|
+
const moving = drag.kind === "move" && container.children.some((child) => child.id === drag.id)
|
|
263
|
+
const index = moving ? container.children.findIndex((child) => child.id === drag.id) : container.children.length
|
|
264
|
+
return { parentId: container.id, index, fill: this.canvas.rectOf(container.id), at: { x: Math.max(0, Math.round((x - rect.left) / unit)), y: Math.max(0, Math.round((y - rect.top) / unit)) } }
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
data/lib/tess_edit/version.rb
CHANGED
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.4.
|
|
4
|
+
version: 0.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -118,6 +118,7 @@ files:
|
|
|
118
118
|
- app/javascript/tess_edit/ui/code_input.js
|
|
119
119
|
- app/javascript/tess_edit/ui/dialog.js
|
|
120
120
|
- app/javascript/tess_edit/ui/dom.js
|
|
121
|
+
- app/javascript/tess_edit/ui/free_layout.js
|
|
121
122
|
- app/javascript/tess_edit/ui/icons.js
|
|
122
123
|
- app/javascript/tess_edit/ui/inspector.js
|
|
123
124
|
- app/javascript/tess_edit/ui/text_editing.js
|