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

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 (3) hide show
  1. package/README.md +0 -2
  2. package/npm/index.js +26 -55
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -95,7 +95,6 @@ const editor = new Editor({
95
95
  },
96
96
  fitToContainerSelector: '.tiptap',
97
97
  autoFitWhenOversized: true,
98
- maxAllowedWidth: null,
99
98
  };
100
99
  },
101
100
  }),
@@ -115,7 +114,6 @@ editor.commands.insertResizableImage({ src: 'https://picsum.photos/300/200', wid
115
114
  - `alignMenuButtonsHide: Record<string, boolean>` — hide specific buttons when `true` (supports `left`, `center`, `right`, `clear`, `preview`, `size50`, `size100`).
116
115
  - `fitToContainerSelector: string` — CSS selector used as width reference for the initial image fit (default: `.tiptap`).
117
116
  - `autoFitWhenOversized: boolean` — when `true`, oversized images inserted without explicit width are automatically set to `width: 100%` and `height: auto` (default: `true`).
118
- - `maxAllowedWidth: number | null` — maximum allowed width in pixels. If image width exceeds this value, it is forced to `width: 100%` and `height: auto`.
119
117
 
120
118
  Reference:
121
119
  - Defined in `resizable-image.js:387-394`
package/npm/index.js CHANGED
@@ -39,11 +39,6 @@ function toFiniteNumber(value) {
39
39
  return Number.isFinite(n) ? n : null
40
40
  }
41
41
 
42
- function isWidth100Percent(value) {
43
- if (value == null) return false
44
- return String(value).trim() === '100%'
45
- }
46
-
47
42
  class ResizableImageView {
48
43
  constructor(node, view, getPos, options) {
49
44
  this.node = node
@@ -153,7 +148,8 @@ class ResizableImageView {
153
148
  if (!hidden.size50) this.menu.appendChild(this.btnSize50)
154
149
  if (!hidden.size100) this.menu.appendChild(this.btnSize100)
155
150
 
156
- this.updateMenuPosition(node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below'))
151
+ const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
152
+ this.updateMenuPosition(menuVerticalPos, node.attrs.align)
157
153
 
158
154
  this.dom.addEventListener('dragstart', (e) => {
159
155
  const dragImg = document.createElement('canvas')
@@ -222,28 +218,13 @@ class ResizableImageView {
222
218
  }
223
219
 
224
220
  applyInitialFitIfNeeded() {
225
- if (!this.img) return
226
-
227
- const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
228
- const shouldAutoFit = this.options?.autoFitWhenOversized !== false
229
- if (!shouldAutoFit && maxAllowedWidth == null) return
230
-
231
- const attrWidth = this.node?.attrs?.width
232
- if (isWidth100Percent(attrWidth)) return
221
+ if (this.options?.autoFitWhenOversized === false) return
222
+ if (this.node?.attrs?.width != null) return
233
223
 
234
224
  const containerWidth = this.getEditorContentWidth()
235
- if (!containerWidth) return
225
+ if (!containerWidth || !this.img?.naturalWidth) return
236
226
 
237
- let limitWidth = shouldAutoFit ? containerWidth : null
238
- if (maxAllowedWidth != null) {
239
- limitWidth = limitWidth == null ? maxAllowedWidth : Math.min(limitWidth, maxAllowedWidth)
240
- }
241
- if (limitWidth == null) return
242
-
243
- const currentWidth = toFiniteNumber(attrWidth) || this.img.naturalWidth || Math.round(this.img.getBoundingClientRect().width)
244
- if (!currentWidth) return
245
-
246
- if (currentWidth <= limitWidth) return
227
+ if (this.img.naturalWidth <= containerWidth) return
247
228
 
248
229
  applyDimensionStyle(this.img, 'width', '100%')
249
230
  applyDimensionStyle(this.img, 'height', 'auto')
@@ -257,9 +238,9 @@ class ResizableImageView {
257
238
  this.view.dispatch(tr)
258
239
  }
259
240
 
260
- updateMenuPosition(pos) {
241
+ updateMenuPosition(verticalPos, align) {
261
242
  this.menu.classList.remove('menu-above', 'menu-below')
262
- if (pos === 'above') {
243
+ if (verticalPos === 'above') {
263
244
  this.menu.classList.add('menu-above')
264
245
  this.menu.style.top = '-32px'
265
246
  this.menu.style.bottom = ''
@@ -268,6 +249,20 @@ class ResizableImageView {
268
249
  this.menu.style.bottom = '-32px'
269
250
  this.menu.style.top = ''
270
251
  }
252
+
253
+ // Horizontal positioning based on image alignment
254
+ this.menu.style.left = ''
255
+ this.menu.style.right = ''
256
+ this.menu.style.transform = ''
257
+ if (align === 'left') {
258
+ this.menu.style.left = '0px'
259
+ } else if (align === 'right') {
260
+ this.menu.style.right = '0px'
261
+ } else {
262
+ // center or default
263
+ this.menu.style.left = '50%'
264
+ this.menu.style.transform = 'translateX(-50%)'
265
+ }
271
266
  }
272
267
 
273
268
  applyAlignAttr(align) {
@@ -296,22 +291,9 @@ class ResizableImageView {
296
291
  }
297
292
 
298
293
  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)
294
+ const percentValue = Math.round(Math.max(0.05, Math.min(1, percent)) * 100) + '%'
302
295
  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)
311
- return
312
- }
313
-
314
- const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: targetW, height: targetH })
296
+ const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: percentValue, height: 'auto' })
315
297
  this.view.dispatch(tr)
316
298
  }
317
299
 
@@ -367,19 +349,8 @@ class ResizableImageView {
367
349
  const finalRect = this.img.getBoundingClientRect()
368
350
  const newWidth = Math.round(finalRect.width)
369
351
  const newHeight = Math.round(finalRect.height)
370
- const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
371
352
  this.dragging = null
372
353
  const pos = this.getPos()
373
- if (maxAllowedWidth != null && newWidth > maxAllowedWidth) {
374
- const tr = this.view.state.tr.setNodeMarkup(pos, null, {
375
- ...this.node.attrs,
376
- width: '100%',
377
- height: 'auto',
378
- })
379
- this.view.dispatch(tr)
380
- return
381
- }
382
-
383
354
  const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: newWidth, height: newHeight })
384
355
  this.view.dispatch(tr)
385
356
  }
@@ -392,7 +363,8 @@ class ResizableImageView {
392
363
  applyDimensionStyle(this.img, 'width', node.attrs.width)
393
364
  applyDimensionStyle(this.img, 'height', node.attrs.height)
394
365
  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'))
366
+ const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
367
+ this.updateMenuPosition(menuVerticalPos, node.attrs.align)
396
368
  this.btnLeft.innerHTML = (node.attrs.iconLeft != null ? node.attrs.iconLeft : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.left : '⟸'))
397
369
  this.btnCenter.innerHTML = (node.attrs.iconCenter != null ? node.attrs.iconCenter : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.center : '⇔'))
398
370
  this.btnRight.innerHTML = (node.attrs.iconRight != null ? node.attrs.iconRight : (this.options && this.options.alignMenuIcons ? this.options.alignMenuIcons.right : '⟹'))
@@ -508,7 +480,6 @@ export const TiptapIzzyExtensionResizableImage = Node.create({
508
480
  alignMenuButtonsHide: {},
509
481
  fitToContainerSelector: '.tiptap',
510
482
  autoFitWhenOversized: true,
511
- maxAllowedWidth: null,
512
483
  }
513
484
  },
514
485
 
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.12",
4
4
  "description": "Resizable image NodeView for TipTap v2 with alignment menu and defaults",
5
5
  "license": "MIT",
6
6
  "type": "module",