tess_edit 0.4.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34a34ebde5272be05d2110672f0de3a54f5d0b1dd3092b0d4af19677376eef0d
4
- data.tar.gz: 709b9b602cee9c5b3c217e8a78d84a25f99358794ba539517587829350ac4de0
3
+ metadata.gz: c56f2e7b17a8fbd7a3ef64e0c12c14858918b5710ba8d0b50a76c26f4eb3f881
4
+ data.tar.gz: f99aadcc71360ee330d3583d5e7caac11514945e2d6c01501070f8cc79eda6b4
5
5
  SHA512:
6
- metadata.gz: 1ccce363718a5d6739d3ce383569cdc1806e244664ad5ed1ca34ac65df38def903d11ee2051c4af8704be49ad033de93dd8a3a2d9f4d1515b9e7b0dce8e756bc
7
- data.tar.gz: a154489406d7669ae43ba0d4bd776b8040663ab8f77541a4b11027feaf3360afa119b5552a653f515a9b1a90ed66e4df6cfbbd2f933520b0382f39231665f457
6
+ metadata.gz: 674212f3ad1388f2c72e4a025c186be0cac74a599d7020d289b3f2e7d31f3fb9c8141ea7215efd9eeac21d3206646deae3b2bba05507d6b70f183821f32151dc
7
+ data.tar.gz: de52504e92d98fa3070159c34253542a42a32a9b226920bef1bfde9cc8b2cbd8245deabd6a51f26a20448fd2bb2e910ada14282cc8a210f086ff935e80f9417d
@@ -49,6 +49,7 @@ import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
49
49
  import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
50
50
  import { Variables, loopScope } from "tess_edit/core/variables"
51
51
  import { Canvas } from "tess_edit/ui/canvas"
52
+ import { freeChild, number } from "tess_edit/ui/free_layout"
52
53
  import { Tree } from "tess_edit/ui/tree"
53
54
  import { Inspector } from "tess_edit/ui/inspector"
54
55
  import { TextEditing } from "tess_edit/ui/text_editing"
@@ -66,6 +67,13 @@ const PANELS_KEY = "tess_edit:panels"
66
67
  const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
67
68
  // Two sidebars (256px + 320px) leave too little canvas below this editor width.
68
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
+ }
69
77
 
70
78
  export default class extends Controller {
71
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 }
@@ -732,20 +740,43 @@ export default class extends Controller {
732
740
  this.tree.clearMarker()
733
741
  }
734
742
 
735
- drop(drag, parentId, index) {
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) {
736
745
  if (!drag || !this.canFill(parentId) || (drag.kind === "move" && !this.isFree(drag.id))) return
737
746
  if (drag.kind === "source") {
738
- this.insertFromSource(drag.source, drag.item, { parentId, index })
747
+ this.insertFromSource(drag.source, drag.item, { parentId, index, at })
739
748
  } else if (drag.kind === "new") {
740
- const node = createFromPalette(drag.key)
749
+ const node = placed(createFromPalette(drag.key), at)
741
750
  this.commit((doc) => insert(doc, parentId, index, node))
742
751
  this.select(node.id)
743
752
  } else {
744
- this.commit((doc) => move(doc, drag.id, parentId, index))
753
+ this.commit((doc) => {
754
+ move(doc, drag.id, parentId, index)
755
+ if (at) placed(find(doc, drag.id).node, at)
756
+ })
745
757
  this.select(drag.id)
746
758
  }
747
759
  }
748
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": number(node.data?.x) + dx, "data.y": number(node.data?.y) + dy }, { coalesce: `${node.id}:nudge` })
777
+ return true
778
+ }
779
+
749
780
  insertAt(key, parentId, index) {
750
781
  if (!this.canFill(parentId)) return
751
782
  const node = createFromPalette(key)
@@ -787,10 +818,10 @@ export default class extends Controller {
787
818
 
788
819
  // Inserts what a palette source's tile creates (see core/registry.js) at a
789
820
  // place, or where a click on a tile would add it
790
- async insertFromSource(source, item, { parentId, index } = {}) {
821
+ async insertFromSource(source, item, { parentId, index, at = null } = {}) {
791
822
  let node
792
823
  try {
793
- node = await source.create(item, this)
824
+ node = placed(await source.create(item, this), at)
794
825
  } catch (error) {
795
826
  this.flash(error.message)
796
827
  return null
@@ -904,6 +935,9 @@ export default class extends Controller {
904
935
  } else if (event.key === "Escape") {
905
936
  this.closeMenu()
906
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()
907
941
  }
908
942
  }
909
943
 
@@ -19,6 +19,13 @@
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
26
+ // resizeChildren: { width: "data.width" },
27
+ // // optional, on a free container: the same for all of its children,
28
+ // // or a function of the child; unit: "mm" keeps sizes as "52mm"
22
29
  // render: { mjml: (node, ctx) => "<mj-text …>" } // one per format of its kits:
23
30
  // // mjml (export/mjml.js), html (export/html.js)
24
31
  // })
@@ -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
- if (editingId !== selectedId) {
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,320 @@
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, or the container's for all of
6
+ // its children, says what their size is:
7
+ //
8
+ // registerBlock({ type: "label", accepts: ["field"], layout: "free", … })
9
+ // registerBlock({ type: "box", roles: ["field"], resize: { width: "data.width", height: "data.height" }, … })
10
+ // registerBlock({ type: "band", accepts: ["content"], layout: "free", resizeChildren: { width: "data.width" }, … })
11
+ //
12
+ // resizeChildren can also be a function of the child, for a size per type
13
+ // of block. A size with a unit (unit: "mm") is kept as text with it, such
14
+ // as an image's attributes.width "52mm"; a value in another unit counts as
15
+ // not set.
16
+ //
17
+ // The container's element says what a unit is: how many units wide it is
18
+ // (data-tess-width="812"), or how many CSS pixels a unit is
19
+ // (data-tess-unit="3.7795" for millimetres); without either a unit is one
20
+ // of its CSS pixels. Positions and sizes are whole units. Edges snap to the
21
+ // container's and the other children's edges and middles, with a guide,
22
+ // and otherwise to the container's grid when it has one
23
+ // (data-tess-grid="5", in units), shown while dragging; Alt turns snapping
24
+ // off. A block dropped from the palette lands where it is let go.
25
+
26
+ import { find, getValue } from "tess_edit/core/document"
27
+ import { getBlock } from "tess_edit/core/registry"
28
+
29
+ // Screen pixels a press moves before it drags, and within which edges snap
30
+ const THRESHOLD = 3
31
+ const SNAP = 5
32
+
33
+ export const FREE_CSS = `
34
+ .tess-guide{position:absolute;background:#ec4899;pointer-events:none}
35
+ .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}
36
+ .tess-free-dragging,.tess-free-dragging *{cursor:grabbing!important;user-select:none}
37
+ .tess-grid{position:absolute;pointer-events:none;background-image:radial-gradient(circle,rgba(37,99,235,.45) 1px,transparent 1.5px)}
38
+ `
39
+
40
+ export function isFreeLayout(node) {
41
+ return getBlock(node?.type)?.layout === "free"
42
+ }
43
+
44
+ // The block in a free container that holds the node, from the node outwards
45
+ export function freeChild(doc, id) {
46
+ let hit = find(doc, id)
47
+ while (hit?.parent) {
48
+ if (isFreeLayout(hit.parent)) return hit
49
+ hit = find(doc, hit.parent.id)
50
+ }
51
+ return null
52
+ }
53
+
54
+ export const number = (value) => {
55
+ const result = Number(value)
56
+ return Number.isFinite(result) ? result : 0
57
+ }
58
+
59
+ // The value on the grid nearest to it, or the value without a grid
60
+ export const onGrid = (value, grid) => (grid > 0 ? Math.round(value / grid) * grid : value)
61
+
62
+ // The size a block resizes, from its definition or its free container's
63
+ export function resizeOf(child) {
64
+ const children = getBlock(child?.parent?.type)?.resizeChildren
65
+ return getBlock(child?.node.type)?.resize || (typeof children === "function" ? children(child.node) : children) || null
66
+ }
67
+
68
+ // A size in the resize's unit as a number, 0 when not set or in another unit
69
+ export function sizeOf(value, unit = "") {
70
+ const text = String(value ?? "").trim()
71
+ if (!unit || !text.endsWith(unit)) return unit ? 0 : number(text)
72
+ return number(text.slice(0, -unit.length).trim())
73
+ }
74
+
75
+ // The nearest of the candidates to one of the edges, within SNAP: the
76
+ // offset that puts it there and where the guide goes
77
+ export function snap(edges, candidates) {
78
+ let best = null
79
+ for (const edge of edges) {
80
+ for (const candidate of candidates) {
81
+ const offset = candidate - edge
82
+ if (Math.abs(offset) <= SNAP && (!best || Math.abs(offset) < Math.abs(best.offset))) best = { offset, at: candidate }
83
+ }
84
+ }
85
+ return best
86
+ }
87
+
88
+ export class FreeLayout {
89
+ constructor(canvas) {
90
+ this.canvas = canvas
91
+ this.gesture = null
92
+ }
93
+
94
+ get editor() {
95
+ return this.canvas.editor
96
+ }
97
+
98
+ get dragging() {
99
+ return !!this.gesture?.active
100
+ }
101
+
102
+ container(parentId) {
103
+ return this.canvas.elementOf(parentId) || (parentId === this.editor.doc.id ? this.canvas.doc.body : null)
104
+ }
105
+
106
+ // Screen pixels per unit of a free container, CSS pixels per screen
107
+ // pixel inside it (the canvas may be zoomed), and its grid in units
108
+ scale(parentId) {
109
+ const element = this.container(parentId)
110
+ const rect = element?.getBoundingClientRect()
111
+ const grid = Number(element?.dataset.tessGrid) || 0
112
+ if (!rect?.width) return { unit: 1, css: 1, rect, grid }
113
+ const css = element.offsetWidth ? element.offsetWidth / rect.width : 1
114
+ const units = Number(element.dataset.tessWidth)
115
+ const cssPerUnit = Number(element.dataset.tessUnit)
116
+ const unit = units > 0 ? rect.width / units : (cssPerUnit > 0 ? cssPerUnit : 1) / css
117
+ return { unit, css, rect, grid }
118
+ }
119
+
120
+ // --- moving and resizing -------------------------------------------------------
121
+
122
+ onPointerDown(event) {
123
+ if (event.button !== 0 || this.editor.editingId || this.canvas.mode !== "edit") return
124
+ if (event.target.closest?.("#tess-overlay")) return
125
+ const hit = this.canvas.hit(event.target)
126
+ if (!hit || hit.placeholder) return
127
+ const child = freeChild(this.editor.doc, hit.id)
128
+ if (child && this.editor.isFree(child.node.id)) this.start(event, "move", child)
129
+ }
130
+
131
+ // The selected block's resize handle, when its definition says what its size is
132
+ handle(id) {
133
+ const child = freeChild(this.editor.doc, id)
134
+ const resize = resizeOf(child)
135
+ if (!resize || !this.editor.isFree(id) || this.editor.editingId) return null
136
+ const handle = this.canvas.doc.createElement("div")
137
+ handle.className = "tess-resize"
138
+ handle.title = "Drag to resize"
139
+ handle.addEventListener("pointerdown", (event) => {
140
+ event.preventDefault()
141
+ event.stopPropagation()
142
+ this.start(event, "resize", child)
143
+ })
144
+ return handle
145
+ }
146
+
147
+ start(event, kind, child) {
148
+ const { node, parent } = child
149
+ const element = this.canvas.elementOf(node.id)
150
+ if (!element) return
151
+ const resize = resizeOf(child) || {}
152
+ const others = parent.children.filter((other) => other.id !== node.id).map((other) => this.canvas.rectOf(other.id)).filter(Boolean)
153
+ const { unit, css, rect: frame, grid } = this.scale(parent.id)
154
+ const rect = element.getBoundingClientRect()
155
+ this.gesture = {
156
+ kind,
157
+ id: node.id,
158
+ element,
159
+ resize,
160
+ unit,
161
+ css,
162
+ grid,
163
+ x: event.clientX,
164
+ y: event.clientY,
165
+ rect,
166
+ size: { width: element.style.width, height: element.style.height },
167
+ // A size not set yet is the size shown
168
+ values: {
169
+ x: number(node.data?.x),
170
+ y: number(node.data?.y),
171
+ width: resize.width ? sizeOf(getValue(node, resize.width), resize.unit) || Math.round(rect.width / unit) : 0,
172
+ height: resize.height ? sizeOf(getValue(node, resize.height), resize.unit) || Math.round(rect.height / unit) : 0
173
+ },
174
+ targets: {
175
+ x: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.left, rect.left + rect.width / 2, rect.right]),
176
+ y: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.top, rect.top + rect.height / 2, rect.bottom])
177
+ },
178
+ frame,
179
+ pointerId: event.pointerId,
180
+ target: event.target
181
+ }
182
+ const doc = this.canvas.doc
183
+ this.onMove = (moveEvent) => this.move(moveEvent)
184
+ this.onUp = (upEvent) => this.end(upEvent)
185
+ doc.addEventListener("pointermove", this.onMove)
186
+ doc.addEventListener("pointerup", this.onUp)
187
+ doc.addEventListener("pointercancel", this.onUp)
188
+ }
189
+
190
+ move(event) {
191
+ const gesture = this.gesture
192
+ if (!gesture || event.pointerId !== gesture.pointerId) return
193
+ let dx = event.clientX - gesture.x
194
+ let dy = event.clientY - gesture.y
195
+ if (!gesture.active) {
196
+ if (Math.hypot(dx, dy) < THRESHOLD) return
197
+ gesture.active = true
198
+ try {
199
+ gesture.target.setPointerCapture?.(gesture.pointerId)
200
+ } catch {}
201
+ this.canvas.doc.documentElement.classList.add("tess-free-dragging")
202
+ this.editor.closeMenu()
203
+ this.editor.select(gesture.id)
204
+ this.canvas.setHover(null)
205
+ }
206
+ event.preventDefault()
207
+ const { rect, unit } = gesture
208
+ const guides = []
209
+ let x = null
210
+ let y = null
211
+ if (!event.altKey) {
212
+ const xs = gesture.kind === "move" ? [rect.left + dx, rect.left + rect.width / 2 + dx, rect.right + dx] : [rect.right + dx]
213
+ const ys = gesture.kind === "move" ? [rect.top + dy, rect.top + rect.height / 2 + dy, rect.bottom + dy] : [rect.bottom + dy]
214
+ x = snap(xs, gesture.targets.x)
215
+ y = snap(ys, gesture.targets.y)
216
+ if (x) {
217
+ dx += x.offset
218
+ guides.push({ left: x.at, top: gesture.frame.top, width: 1, height: gesture.frame.height })
219
+ }
220
+ if (y) {
221
+ dy += y.offset
222
+ guides.push({ left: gesture.frame.left, top: y.at, width: gesture.frame.width, height: 1 })
223
+ }
224
+ }
225
+ // Whole units, shown as they will be; on the grid when no edge snapped
226
+ const grid = event.altKey ? 0 : gesture.grid
227
+ const start = gesture.kind === "move" ? gesture.values : { x: gesture.values.width, y: gesture.values.height }
228
+ const du = x || !grid ? Math.round(dx / unit) : onGrid(start.x + dx / unit, grid) - start.x
229
+ const dv = y || !grid ? Math.round(dy / unit) : onGrid(start.y + dy / unit, grid) - start.y
230
+ const px = (units) => `${units * gesture.unit * gesture.css}px`
231
+ if (gesture.kind === "move") {
232
+ gesture.change = { x: gesture.values.x + du, y: gesture.values.y + dv }
233
+ gesture.element.style.translate = `${px(du)} ${px(dv)}`
234
+ } else {
235
+ gesture.change = this.resized(gesture, du, dv)
236
+ const width = gesture.resize.width ? gesture.change.width - gesture.values.width : 0
237
+ const height = gesture.resize.height ? gesture.change.height - gesture.values.height : 0
238
+ gesture.element.style.width = `calc(${px(rect.width / gesture.unit)} + ${px(width)})`
239
+ gesture.element.style.height = `calc(${px(rect.height / gesture.unit)} + ${px(height)})`
240
+ }
241
+ this.guides = guides
242
+ this.grid = grid * unit >= 4 && gesture.frame ? { rect: gesture.frame, step: grid * unit } : null
243
+ this.canvas.refresh()
244
+ }
245
+
246
+ // The new size; one key for both (a circle's diameter) takes the larger change
247
+ resized(gesture, du, dv) {
248
+ const { width, height } = gesture.values
249
+ if (gesture.resize.width && gesture.resize.width === gesture.resize.height) {
250
+ const size = Math.max(1, width + (Math.abs(du) > Math.abs(dv) ? du : dv))
251
+ return { width: size, height: size }
252
+ }
253
+ return { width: Math.max(1, width + du), height: Math.max(1, height + dv) }
254
+ }
255
+
256
+ end(event) {
257
+ const gesture = this.gesture
258
+ if (!gesture || event.pointerId !== gesture.pointerId) return
259
+ const doc = this.canvas.doc
260
+ doc.removeEventListener("pointermove", this.onMove)
261
+ doc.removeEventListener("pointerup", this.onUp)
262
+ doc.removeEventListener("pointercancel", this.onUp)
263
+ doc.documentElement.classList.remove("tess-free-dragging")
264
+ this.gesture = null
265
+ this.guides = []
266
+ this.grid = null
267
+ if (!gesture.active) return
268
+ // The click that ends a drag isn't one (see swallowClick)
269
+ this.dragged = true
270
+ setTimeout(() => (this.dragged = false))
271
+ gesture.element.style.translate = ""
272
+ Object.assign(gesture.element.style, gesture.size)
273
+ const change = gesture.change || {}
274
+ if (event.type === "pointercancel") return this.canvas.refresh()
275
+ if (gesture.kind === "move") this.editor.place(gesture.id, { "data.x": change.x, "data.y": change.y })
276
+ else {
277
+ const size = (value) => (gesture.resize.unit ? `${value}${gesture.resize.unit}` : value)
278
+ this.editor.place(gesture.id, { [gesture.resize.width]: size(change.width), [gesture.resize.height]: size(change.height) })
279
+ }
280
+ this.canvas.refresh()
281
+ }
282
+
283
+ // Whether a click ends a drag, and so selects or edits nothing
284
+ swallowClick() {
285
+ const dragged = this.dragged
286
+ this.dragged = false
287
+ return dragged
288
+ }
289
+
290
+ // --- the overlay -----------------------------------------------------------------
291
+
292
+ drawGuides(overlay) {
293
+ if (this.grid) {
294
+ const { rect, step } = this.grid
295
+ const box = this.canvas.box(rect, "tess-grid")
296
+ Object.assign(box.style, { backgroundSize: `${step}px ${step}px`, backgroundPosition: `${-step / 2}px ${-step / 2}px` })
297
+ overlay.append(box)
298
+ }
299
+ for (const rect of this.guides || []) overlay.append(this.canvas.box(rect, "tess-guide"))
300
+ }
301
+
302
+ // --- dropping from the palette -------------------------------------------------
303
+
304
+ // A free container under the pointer that takes the dragged block, and
305
+ // where in it, in units
306
+ dropTarget(x, y, drag) {
307
+ const doc = this.editor.doc
308
+ const hit = this.canvas.hit(this.canvas.doc.elementFromPoint(x, y))
309
+ let current = find(doc, hit ? hit.id : doc.id)
310
+ while (current && !isFreeLayout(current.node)) current = current.parent && find(doc, current.parent.id)
311
+ if (!current || !this.canvas.allowed(current.node.id, drag)) return null
312
+ const container = current.node
313
+ const { unit, rect, grid } = this.scale(container.id)
314
+ if (!rect) return null
315
+ const moving = drag.kind === "move" && container.children.some((child) => child.id === drag.id)
316
+ const index = moving ? container.children.findIndex((child) => child.id === drag.id) : container.children.length
317
+ return { parentId: container.id, index, fill: this.canvas.rectOf(container.id), at: { x: Math.max(0, Math.round(onGrid((x - rect.left) / unit, grid))), y: Math.max(0, Math.round(onGrid((y - rect.top) / unit, grid))) } }
318
+ }
319
+ }
320
+
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.3"
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.4.1
4
+ version: 0.4.3
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