tiptap-extension-izzy-resizable-image 0.1.11 → 0.1.13

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.
Files changed (2) hide show
  1. package/npm/index.js +41 -20
  2. package/package.json +1 -1
package/npm/index.js CHANGED
@@ -44,6 +44,11 @@ function isWidth100Percent(value) {
44
44
  return String(value).trim() === '100%'
45
45
  }
46
46
 
47
+ function isPercentWidth(value) {
48
+ if (value == null) return false
49
+ return /^\d+(\.\d+)?%$/.test(String(value).trim())
50
+ }
51
+
47
52
  class ResizableImageView {
48
53
  constructor(node, view, getPos, options) {
49
54
  this.node = node
@@ -80,7 +85,7 @@ class ResizableImageView {
80
85
  this.img.style.pointerEvents = 'none'
81
86
  if (node.attrs.id) this.img.id = node.attrs.id
82
87
  if (node.attrs.class) this.img.className = node.attrs.class
83
- applyDimensionStyle(this.img, 'width', node.attrs.width)
88
+ this.applyWidthLayout(node.attrs.width)
84
89
  if (node.attrs.height) {
85
90
  applyDimensionStyle(this.img, 'height', node.attrs.height)
86
91
  } else if (this.options && this.options.height) {
@@ -153,7 +158,8 @@ class ResizableImageView {
153
158
  if (!hidden.size50) this.menu.appendChild(this.btnSize50)
154
159
  if (!hidden.size100) this.menu.appendChild(this.btnSize100)
155
160
 
156
- this.updateMenuPosition(node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below'))
161
+ const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
162
+ this.updateMenuPosition(menuVerticalPos, node.attrs.align)
157
163
 
158
164
  this.dom.addEventListener('dragstart', (e) => {
159
165
  const dragImg = document.createElement('canvas')
@@ -257,9 +263,9 @@ class ResizableImageView {
257
263
  this.view.dispatch(tr)
258
264
  }
259
265
 
260
- updateMenuPosition(pos) {
266
+ updateMenuPosition(verticalPos, align) {
261
267
  this.menu.classList.remove('menu-above', 'menu-below')
262
- if (pos === 'above') {
268
+ if (verticalPos === 'above') {
263
269
  this.menu.classList.add('menu-above')
264
270
  this.menu.style.top = '-32px'
265
271
  this.menu.style.bottom = ''
@@ -268,6 +274,21 @@ class ResizableImageView {
268
274
  this.menu.style.bottom = '-32px'
269
275
  this.menu.style.top = ''
270
276
  }
277
+
278
+ // Horizontal positioning based on image alignment
279
+ this.menu.style.left = ''
280
+ this.menu.style.right = ''
281
+ this.menu.style.transform = ''
282
+ if (align === 'right') {
283
+ this.menu.style.right = '0px'
284
+ } else if (align === 'center') {
285
+ // Explicit center alignment
286
+ this.menu.style.left = '50%'
287
+ this.menu.style.transform = 'translateX(-50%)'
288
+ } else {
289
+ // left or default (no align)
290
+ this.menu.style.left = '0px'
291
+ }
271
292
  }
272
293
 
273
294
  applyAlignAttr(align) {
@@ -295,23 +316,21 @@ class ResizableImageView {
295
316
  }
296
317
  }
297
318
 
298
- applyResizePercent(percent) {
299
- const naturalW = this.img.naturalWidth || Math.round(this.img.getBoundingClientRect().width)
300
- const targetW = Math.max(20, Math.round(naturalW * percent))
301
- const targetH = this.aspect ? Math.round(targetW / this.aspect) : Math.round(this.img.getBoundingClientRect().height)
302
- const pos = this.getPos()
303
- const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
304
- if (maxAllowedWidth != null && targetW > maxAllowedWidth) {
305
- const tr = this.view.state.tr.setNodeMarkup(pos, null, {
306
- ...this.node.attrs,
307
- width: '100%',
308
- height: 'auto',
309
- })
310
- this.view.dispatch(tr)
319
+ applyWidthLayout(widthValue) {
320
+ if (isPercentWidth(widthValue)) {
321
+ applyDimensionStyle(this.inner, 'width', widthValue)
322
+ applyDimensionStyle(this.img, 'width', '100%')
311
323
  return
312
324
  }
313
325
 
314
- const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: targetW, height: targetH })
326
+ this.inner.style.removeProperty('width')
327
+ applyDimensionStyle(this.img, 'width', widthValue)
328
+ }
329
+
330
+ applyResizePercent(percent) {
331
+ const percentValue = Math.round(Math.max(0.05, Math.min(1, percent)) * 100) + '%'
332
+ const pos = this.getPos()
333
+ const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: percentValue, height: 'auto' })
315
334
  this.view.dispatch(tr)
316
335
  }
317
336
 
@@ -356,6 +375,7 @@ class ResizableImageView {
356
375
  newHeight = Math.round(newWidth / this.aspect)
357
376
  }
358
377
  }
378
+ this.inner.style.width = newWidth + 'px'
359
379
  this.img.style.width = newWidth + 'px'
360
380
  this.img.style.height = newHeight + 'px'
361
381
  }
@@ -389,10 +409,11 @@ class ResizableImageView {
389
409
  this.node = node
390
410
  if (node.attrs.src !== this.img.src) this.img.src = node.attrs.src
391
411
  this.img.alt = node.attrs.alt || ''
392
- applyDimensionStyle(this.img, 'width', node.attrs.width)
412
+ this.applyWidthLayout(node.attrs.width)
393
413
  applyDimensionStyle(this.img, 'height', node.attrs.height)
394
414
  this.applyAlignment(node.attrs.align)
395
- this.updateMenuPosition(node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below'))
415
+ const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
416
+ this.updateMenuPosition(menuVerticalPos, node.attrs.align)
396
417
  this.btnLeft.innerHTML = (node.attrs.iconLeft != null ? node.attrs.iconLeft : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.left : '⟸'))
397
418
  this.btnCenter.innerHTML = (node.attrs.iconCenter != null ? node.attrs.iconCenter : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.center : '⇔'))
398
419
  this.btnRight.innerHTML = (node.attrs.iconRight != null ? node.attrs.iconRight : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.right : '⟹'))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiptap-extension-izzy-resizable-image",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Resizable image NodeView for TipTap v2 with alignment menu and defaults",
5
5
  "license": "MIT",
6
6
  "type": "module",