tess_edit 0.4.2 → 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: b135e56fcad4ed593a2126a6196f218007859254ab97999b65864e5600764335
4
- data.tar.gz: 04cad6e0e9b1bd1b96043dee46715db7d4dcccfee69ab9c0922735b225341aa5
3
+ metadata.gz: c56f2e7b17a8fbd7a3ef64e0c12c14858918b5710ba8d0b50a76c26f4eb3f881
4
+ data.tar.gz: f99aadcc71360ee330d3583d5e7caac11514945e2d6c01501070f8cc79eda6b4
5
5
  SHA512:
6
- metadata.gz: aa237f8f3887618415fefe2c27a1a507eb2f112f30aad0ed1a4c62be60b50edd0b0320367a98890a3a228f66cbcc8e1df2a5e8596854e5a0a9114ef7d88a84a2
7
- data.tar.gz: 2d45642436414759612252f86b67ccde984118d5f2fe16cb0de01e8ab9cc82ed30e508f81adb5951492e983a527eae9d7a2a0c405425908145d45d8791b133b1
6
+ metadata.gz: 674212f3ad1388f2c72e4a025c186be0cac74a599d7020d289b3f2e7d31f3fb9c8141ea7215efd9eeac21d3206646deae3b2bba05507d6b70f183821f32151dc
7
+ data.tar.gz: de52504e92d98fa3070159c34253542a42a32a9b226920bef1bfde9cc8b2cbd8245deabd6a51f26a20448fd2bb2e910ada14282cc8a210f086ff935e80f9417d
@@ -773,7 +773,7 @@ export default class extends Controller {
773
773
  const child = freeChild(this.doc, this.selectedId)
774
774
  if (!child || !this.isFree(child.node.id)) return false
775
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` })
776
+ this.place(node.id, { "data.x": number(node.data?.x) + dx, "data.y": number(node.data?.y) + dy }, { coalesce: `${node.id}:nudge` })
777
777
  return true
778
778
  }
779
779
 
@@ -23,6 +23,9 @@
23
23
  // // data.x and data.y, dragged on the canvas (ui/free_layout.js)
24
24
  // resize: { width: "data.width", height: "data.height" },
25
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"
26
29
  // render: { mjml: (node, ctx) => "<mj-text …>" } // one per format of its kits:
27
30
  // // mjml (export/mjml.js), html (export/html.js)
28
31
  // })
@@ -2,16 +2,26 @@
2
2
  // children by x and y (data.x, data.y, in the kit's own units, such as a
3
3
  // label's dots) instead of one after another. On the canvas its children
4
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:
5
+ // resized by a handle when their definition, or the container's for all of
6
+ // its children, says what their size is:
6
7
  //
7
8
  // registerBlock({ type: "label", accepts: ["field"], layout: "free", … })
8
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" }, … })
9
11
  //
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.
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.
15
25
 
16
26
  import { find, getValue } from "tess_edit/core/document"
17
27
  import { getBlock } from "tess_edit/core/registry"
@@ -24,6 +34,7 @@ export const FREE_CSS = `
24
34
  .tess-guide{position:absolute;background:#ec4899;pointer-events:none}
25
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}
26
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)}
27
38
  `
28
39
 
29
40
  export function isFreeLayout(node) {
@@ -45,6 +56,22 @@ export const number = (value) => {
45
56
  return Number.isFinite(result) ? result : 0
46
57
  }
47
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
+
48
75
  // The nearest of the candidates to one of the edges, within SNAP: the
49
76
  // offset that puts it there and where the guide goes
50
77
  export function snap(edges, candidates) {
@@ -76,15 +103,18 @@ export class FreeLayout {
76
103
  return this.canvas.elementOf(parentId) || (parentId === this.editor.doc.id ? this.canvas.doc.body : null)
77
104
  }
78
105
 
79
- // Screen pixels per unit of a free container, and CSS pixels per screen
80
- // pixel inside it (the canvas may be zoomed)
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
81
108
  scale(parentId) {
82
109
  const element = this.container(parentId)
83
110
  const rect = element?.getBoundingClientRect()
84
- if (!rect?.width) return { unit: 1, css: 1, rect }
111
+ const grid = Number(element?.dataset.tessGrid) || 0
112
+ if (!rect?.width) return { unit: 1, css: 1, rect, grid }
85
113
  const css = element.offsetWidth ? element.offsetWidth / rect.width : 1
86
114
  const units = Number(element.dataset.tessWidth)
87
- return { unit: units > 0 ? rect.width / units : 1 / css, css, rect }
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 }
88
118
  }
89
119
 
90
120
  // --- moving and resizing -------------------------------------------------------
@@ -101,7 +131,7 @@ export class FreeLayout {
101
131
  // The selected block's resize handle, when its definition says what its size is
102
132
  handle(id) {
103
133
  const child = freeChild(this.editor.doc, id)
104
- const resize = child && getBlock(child.node.type)?.resize
134
+ const resize = resizeOf(child)
105
135
  if (!resize || !this.editor.isFree(id) || this.editor.editingId) return null
106
136
  const handle = this.canvas.doc.createElement("div")
107
137
  handle.className = "tess-resize"
@@ -118,9 +148,10 @@ export class FreeLayout {
118
148
  const { node, parent } = child
119
149
  const element = this.canvas.elementOf(node.id)
120
150
  if (!element) return
121
- const resize = getBlock(node.type)?.resize || {}
151
+ const resize = resizeOf(child) || {}
122
152
  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)
153
+ const { unit, css, rect: frame, grid } = this.scale(parent.id)
154
+ const rect = element.getBoundingClientRect()
124
155
  this.gesture = {
125
156
  kind,
126
157
  id: node.id,
@@ -128,11 +159,18 @@ export class FreeLayout {
128
159
  resize,
129
160
  unit,
130
161
  css,
162
+ grid,
131
163
  x: event.clientX,
132
164
  y: event.clientY,
133
- rect: element.getBoundingClientRect(),
165
+ rect,
134
166
  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 },
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
+ },
136
174
  targets: {
137
175
  x: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.left, rect.left + rect.width / 2, rect.right]),
138
176
  y: [frame, ...others].filter(Boolean).flatMap((rect) => [rect.top, rect.top + rect.height / 2, rect.bottom])
@@ -166,13 +204,15 @@ export class FreeLayout {
166
204
  this.canvas.setHover(null)
167
205
  }
168
206
  event.preventDefault()
169
- const { rect } = gesture
207
+ const { rect, unit } = gesture
170
208
  const guides = []
209
+ let x = null
210
+ let y = null
171
211
  if (!event.altKey) {
172
212
  const xs = gesture.kind === "move" ? [rect.left + dx, rect.left + rect.width / 2 + dx, rect.right + dx] : [rect.right + dx]
173
213
  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)
214
+ x = snap(xs, gesture.targets.x)
215
+ y = snap(ys, gesture.targets.y)
176
216
  if (x) {
177
217
  dx += x.offset
178
218
  guides.push({ left: x.at, top: gesture.frame.top, width: 1, height: gesture.frame.height })
@@ -182,9 +222,11 @@ export class FreeLayout {
182
222
  guides.push({ left: gesture.frame.left, top: y.at, width: gesture.frame.width, height: 1 })
183
223
  }
184
224
  }
185
- // Whole units, shown as they will be
186
- const du = Math.round(dx / gesture.unit)
187
- const dv = Math.round(dy / gesture.unit)
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
188
230
  const px = (units) => `${units * gesture.unit * gesture.css}px`
189
231
  if (gesture.kind === "move") {
190
232
  gesture.change = { x: gesture.values.x + du, y: gesture.values.y + dv }
@@ -197,6 +239,7 @@ export class FreeLayout {
197
239
  gesture.element.style.height = `calc(${px(rect.height / gesture.unit)} + ${px(height)})`
198
240
  }
199
241
  this.guides = guides
242
+ this.grid = grid * unit >= 4 && gesture.frame ? { rect: gesture.frame, step: grid * unit } : null
200
243
  this.canvas.refresh()
201
244
  }
202
245
 
@@ -220,6 +263,7 @@ export class FreeLayout {
220
263
  doc.documentElement.classList.remove("tess-free-dragging")
221
264
  this.gesture = null
222
265
  this.guides = []
266
+ this.grid = null
223
267
  if (!gesture.active) return
224
268
  // The click that ends a drag isn't one (see swallowClick)
225
269
  this.dragged = true
@@ -229,7 +273,10 @@ export class FreeLayout {
229
273
  const change = gesture.change || {}
230
274
  if (event.type === "pointercancel") return this.canvas.refresh()
231
275
  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 })
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
+ }
233
280
  this.canvas.refresh()
234
281
  }
235
282
 
@@ -243,6 +290,12 @@ export class FreeLayout {
243
290
  // --- the overlay -----------------------------------------------------------------
244
291
 
245
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
+ }
246
299
  for (const rect of this.guides || []) overlay.append(this.canvas.box(rect, "tess-guide"))
247
300
  }
248
301
 
@@ -257,11 +310,11 @@ export class FreeLayout {
257
310
  while (current && !isFreeLayout(current.node)) current = current.parent && find(doc, current.parent.id)
258
311
  if (!current || !this.canvas.allowed(current.node.id, drag)) return null
259
312
  const container = current.node
260
- const { unit, rect } = this.scale(container.id)
313
+ const { unit, rect, grid } = this.scale(container.id)
261
314
  if (!rect) return null
262
315
  const moving = drag.kind === "move" && container.children.some((child) => child.id === drag.id)
263
316
  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)) } }
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))) } }
265
318
  }
266
319
  }
267
320
 
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.4.2"
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.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt