tiptap-extension-izzy-resizable-image 0.1.12 → 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 (3) hide show
  1. package/README.md +2 -0
  2. package/npm/index.js +61 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -95,6 +95,7 @@ const editor = new Editor({
95
95
  },
96
96
  fitToContainerSelector: '.tiptap',
97
97
  autoFitWhenOversized: true,
98
+ maxAllowedWidth: null,
98
99
  };
99
100
  },
100
101
  }),
@@ -114,6 +115,7 @@ editor.commands.insertResizableImage({ src: 'https://picsum.photos/300/200', wid
114
115
  - `alignMenuButtonsHide: Record<string, boolean>` — hide specific buttons when `true` (supports `left`, `center`, `right`, `clear`, `preview`, `size50`, `size100`).
115
116
  - `fitToContainerSelector: string` — CSS selector used as width reference for the initial image fit (default: `.tiptap`).
116
117
  - `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`.
117
119
 
118
120
  Reference:
119
121
  - Defined in `resizable-image.js:387-394`
package/npm/index.js CHANGED
@@ -39,6 +39,16 @@ 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
+ function isPercentWidth(value) {
48
+ if (value == null) return false
49
+ return /^\d+(\.\d+)?%$/.test(String(value).trim())
50
+ }
51
+
42
52
  class ResizableImageView {
43
53
  constructor(node, view, getPos, options) {
44
54
  this.node = node
@@ -75,7 +85,7 @@ class ResizableImageView {
75
85
  this.img.style.pointerEvents = 'none'
76
86
  if (node.attrs.id) this.img.id = node.attrs.id
77
87
  if (node.attrs.class) this.img.className = node.attrs.class
78
- applyDimensionStyle(this.img, 'width', node.attrs.width)
88
+ this.applyWidthLayout(node.attrs.width)
79
89
  if (node.attrs.height) {
80
90
  applyDimensionStyle(this.img, 'height', node.attrs.height)
81
91
  } else if (this.options && this.options.height) {
@@ -218,13 +228,28 @@ class ResizableImageView {
218
228
  }
219
229
 
220
230
  applyInitialFitIfNeeded() {
221
- if (this.options?.autoFitWhenOversized === false) return
222
- if (this.node?.attrs?.width != null) return
231
+ if (!this.img) return
232
+
233
+ const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
234
+ const shouldAutoFit = this.options?.autoFitWhenOversized !== false
235
+ if (!shouldAutoFit && maxAllowedWidth == null) return
236
+
237
+ const attrWidth = this.node?.attrs?.width
238
+ if (isWidth100Percent(attrWidth)) return
223
239
 
224
240
  const containerWidth = this.getEditorContentWidth()
225
- if (!containerWidth || !this.img?.naturalWidth) return
241
+ if (!containerWidth) return
242
+
243
+ let limitWidth = shouldAutoFit ? containerWidth : null
244
+ if (maxAllowedWidth != null) {
245
+ limitWidth = limitWidth == null ? maxAllowedWidth : Math.min(limitWidth, maxAllowedWidth)
246
+ }
247
+ if (limitWidth == null) return
226
248
 
227
- if (this.img.naturalWidth <= containerWidth) return
249
+ const currentWidth = toFiniteNumber(attrWidth) || this.img.naturalWidth || Math.round(this.img.getBoundingClientRect().width)
250
+ if (!currentWidth) return
251
+
252
+ if (currentWidth <= limitWidth) return
228
253
 
229
254
  applyDimensionStyle(this.img, 'width', '100%')
230
255
  applyDimensionStyle(this.img, 'height', 'auto')
@@ -254,14 +279,15 @@ class ResizableImageView {
254
279
  this.menu.style.left = ''
255
280
  this.menu.style.right = ''
256
281
  this.menu.style.transform = ''
257
- if (align === 'left') {
258
- this.menu.style.left = '0px'
259
- } else if (align === 'right') {
282
+ if (align === 'right') {
260
283
  this.menu.style.right = '0px'
261
- } else {
262
- // center or default
284
+ } else if (align === 'center') {
285
+ // Explicit center alignment
263
286
  this.menu.style.left = '50%'
264
287
  this.menu.style.transform = 'translateX(-50%)'
288
+ } else {
289
+ // left or default (no align)
290
+ this.menu.style.left = '0px'
265
291
  }
266
292
  }
267
293
 
@@ -290,6 +316,17 @@ class ResizableImageView {
290
316
  }
291
317
  }
292
318
 
319
+ applyWidthLayout(widthValue) {
320
+ if (isPercentWidth(widthValue)) {
321
+ applyDimensionStyle(this.inner, 'width', widthValue)
322
+ applyDimensionStyle(this.img, 'width', '100%')
323
+ return
324
+ }
325
+
326
+ this.inner.style.removeProperty('width')
327
+ applyDimensionStyle(this.img, 'width', widthValue)
328
+ }
329
+
293
330
  applyResizePercent(percent) {
294
331
  const percentValue = Math.round(Math.max(0.05, Math.min(1, percent)) * 100) + '%'
295
332
  const pos = this.getPos()
@@ -338,6 +375,7 @@ class ResizableImageView {
338
375
  newHeight = Math.round(newWidth / this.aspect)
339
376
  }
340
377
  }
378
+ this.inner.style.width = newWidth + 'px'
341
379
  this.img.style.width = newWidth + 'px'
342
380
  this.img.style.height = newHeight + 'px'
343
381
  }
@@ -349,8 +387,19 @@ class ResizableImageView {
349
387
  const finalRect = this.img.getBoundingClientRect()
350
388
  const newWidth = Math.round(finalRect.width)
351
389
  const newHeight = Math.round(finalRect.height)
390
+ const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
352
391
  this.dragging = null
353
392
  const pos = this.getPos()
393
+ if (maxAllowedWidth != null && newWidth > maxAllowedWidth) {
394
+ const tr = this.view.state.tr.setNodeMarkup(pos, null, {
395
+ ...this.node.attrs,
396
+ width: '100%',
397
+ height: 'auto',
398
+ })
399
+ this.view.dispatch(tr)
400
+ return
401
+ }
402
+
354
403
  const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: newWidth, height: newHeight })
355
404
  this.view.dispatch(tr)
356
405
  }
@@ -360,7 +409,7 @@ class ResizableImageView {
360
409
  this.node = node
361
410
  if (node.attrs.src !== this.img.src) this.img.src = node.attrs.src
362
411
  this.img.alt = node.attrs.alt || ''
363
- applyDimensionStyle(this.img, 'width', node.attrs.width)
412
+ this.applyWidthLayout(node.attrs.width)
364
413
  applyDimensionStyle(this.img, 'height', node.attrs.height)
365
414
  this.applyAlignment(node.attrs.align)
366
415
  const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
@@ -480,6 +529,7 @@ export const TiptapIzzyExtensionResizableImage = Node.create({
480
529
  alignMenuButtonsHide: {},
481
530
  fitToContainerSelector: '.tiptap',
482
531
  autoFitWhenOversized: true,
532
+ maxAllowedWidth: null,
483
533
  }
484
534
  },
485
535
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiptap-extension-izzy-resizable-image",
3
- "version": "0.1.12",
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",