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

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 +4 -0
  2. package/npm/index.js +81 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -83,6 +83,7 @@ const editor = new Editor({
83
83
  clear: 'x',
84
84
  preview: '🔍',
85
85
  },
86
+ alignMenuOrder: ['left', 'center', 'right', 'clear', 'preview', 'size50', 'size100'],
86
87
  alignMenuButtonsHide: {
87
88
  // hide specific buttons if true
88
89
  // left: true,
@@ -95,6 +96,7 @@ const editor = new Editor({
95
96
  },
96
97
  fitToContainerSelector: '.tiptap',
97
98
  autoFitWhenOversized: true,
99
+ maxAllowedWidth: null,
98
100
  };
99
101
  },
100
102
  }),
@@ -111,9 +113,11 @@ editor.commands.insertResizableImage({ src: 'https://picsum.photos/300/200', wid
111
113
  - `showAlignMenu: boolean` — whether the inline alignment menu is shown by default.
112
114
  - `alignMenuPosition: 'above' | 'below'` — where the menu appears relative to the image.
113
115
  - `alignMenuIcons: { left: string, center: string, right: string, clear: string, preview: string }` — HTML strings for menu buttons.
116
+ - `alignMenuOrder: string[]` — order of buttons in the alignment menu (default: `['left', 'center', 'right', 'clear', 'preview', 'size50', 'size100']`). Buttons are displayed in the order specified.
114
117
  - `alignMenuButtonsHide: Record<string, boolean>` — hide specific buttons when `true` (supports `left`, `center`, `right`, `clear`, `preview`, `size50`, `size100`).
115
118
  - `fitToContainerSelector: string` — CSS selector used as width reference for the initial image fit (default: `.tiptap`).
116
119
  - `autoFitWhenOversized: boolean` — when `true`, oversized images inserted without explicit width are automatically set to `width: 100%` and `height: auto` (default: `true`).
120
+ - `maxAllowedWidth: number | null` — maximum allowed width in pixels. If image width exceeds this value, it is forced to `width: 100%` and `height: auto`.
117
121
 
118
122
  Reference:
119
123
  - 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) {
@@ -139,14 +149,26 @@ class ResizableImageView {
139
149
  this.btnSize50.addEventListener('click', () => this.applyResizePercent(0.5))
140
150
  this.btnSize100.addEventListener('click', () => this.applyResizePercent(1))
141
151
 
152
+ // Button map for ordering
153
+ const buttonMap = {
154
+ 'left': this.btnLeft,
155
+ 'center': this.btnCenter,
156
+ 'right': this.btnRight,
157
+ 'clear': this.btnClear,
158
+ 'preview': this.btnView,
159
+ 'size50': this.btnSize50,
160
+ 'size100': this.btnSize100,
161
+ }
162
+
163
+ // Append buttons in the order specified by alignMenuOrder
142
164
  const hidden = this.options.alignMenuButtonsHide || {}
143
- if (!hidden.left) this.menu.appendChild(this.btnLeft)
144
- if (!hidden.center) this.menu.appendChild(this.btnCenter)
145
- if (!hidden.right) this.menu.appendChild(this.btnRight)
146
- if (!hidden.clear) this.menu.appendChild(this.btnClear)
147
- if (!hidden.preview) this.menu.appendChild(this.btnView)
148
- if (!hidden.size50) this.menu.appendChild(this.btnSize50)
149
- if (!hidden.size100) this.menu.appendChild(this.btnSize100)
165
+ const order = this.options?.alignMenuOrder || ['left', 'center', 'right', 'clear', 'preview', 'size50', 'size100']
166
+ order.forEach(btnName => {
167
+ const btn = buttonMap[btnName]
168
+ if (btn && !hidden[btnName]) {
169
+ this.menu.appendChild(btn)
170
+ }
171
+ })
150
172
 
151
173
  const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
152
174
  this.updateMenuPosition(menuVerticalPos, node.attrs.align)
@@ -218,13 +240,28 @@ class ResizableImageView {
218
240
  }
219
241
 
220
242
  applyInitialFitIfNeeded() {
221
- if (this.options?.autoFitWhenOversized === false) return
222
- if (this.node?.attrs?.width != null) return
243
+ if (!this.img) return
244
+
245
+ const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
246
+ const shouldAutoFit = this.options?.autoFitWhenOversized !== false
247
+ if (!shouldAutoFit && maxAllowedWidth == null) return
248
+
249
+ const attrWidth = this.node?.attrs?.width
250
+ if (isWidth100Percent(attrWidth)) return
223
251
 
224
252
  const containerWidth = this.getEditorContentWidth()
225
- if (!containerWidth || !this.img?.naturalWidth) return
253
+ if (!containerWidth) return
254
+
255
+ let limitWidth = shouldAutoFit ? containerWidth : null
256
+ if (maxAllowedWidth != null) {
257
+ limitWidth = limitWidth == null ? maxAllowedWidth : Math.min(limitWidth, maxAllowedWidth)
258
+ }
259
+ if (limitWidth == null) return
226
260
 
227
- if (this.img.naturalWidth <= containerWidth) return
261
+ const currentWidth = toFiniteNumber(attrWidth) || this.img.naturalWidth || Math.round(this.img.getBoundingClientRect().width)
262
+ if (!currentWidth) return
263
+
264
+ if (currentWidth <= limitWidth) return
228
265
 
229
266
  applyDimensionStyle(this.img, 'width', '100%')
230
267
  applyDimensionStyle(this.img, 'height', 'auto')
@@ -254,14 +291,15 @@ class ResizableImageView {
254
291
  this.menu.style.left = ''
255
292
  this.menu.style.right = ''
256
293
  this.menu.style.transform = ''
257
- if (align === 'left') {
258
- this.menu.style.left = '0px'
259
- } else if (align === 'right') {
294
+ if (align === 'right') {
260
295
  this.menu.style.right = '0px'
261
- } else {
262
- // center or default
296
+ } else if (align === 'center') {
297
+ // Explicit center alignment
263
298
  this.menu.style.left = '50%'
264
299
  this.menu.style.transform = 'translateX(-50%)'
300
+ } else {
301
+ // left or default (no align)
302
+ this.menu.style.left = '0px'
265
303
  }
266
304
  }
267
305
 
@@ -290,6 +328,17 @@ class ResizableImageView {
290
328
  }
291
329
  }
292
330
 
331
+ applyWidthLayout(widthValue) {
332
+ if (isPercentWidth(widthValue)) {
333
+ applyDimensionStyle(this.inner, 'width', widthValue)
334
+ applyDimensionStyle(this.img, 'width', '100%')
335
+ return
336
+ }
337
+
338
+ this.inner.style.removeProperty('width')
339
+ applyDimensionStyle(this.img, 'width', widthValue)
340
+ }
341
+
293
342
  applyResizePercent(percent) {
294
343
  const percentValue = Math.round(Math.max(0.05, Math.min(1, percent)) * 100) + '%'
295
344
  const pos = this.getPos()
@@ -338,6 +387,7 @@ class ResizableImageView {
338
387
  newHeight = Math.round(newWidth / this.aspect)
339
388
  }
340
389
  }
390
+ this.inner.style.width = newWidth + 'px'
341
391
  this.img.style.width = newWidth + 'px'
342
392
  this.img.style.height = newHeight + 'px'
343
393
  }
@@ -349,8 +399,19 @@ class ResizableImageView {
349
399
  const finalRect = this.img.getBoundingClientRect()
350
400
  const newWidth = Math.round(finalRect.width)
351
401
  const newHeight = Math.round(finalRect.height)
402
+ const maxAllowedWidth = toFiniteNumber(this.options?.maxAllowedWidth)
352
403
  this.dragging = null
353
404
  const pos = this.getPos()
405
+ if (maxAllowedWidth != null && newWidth > maxAllowedWidth) {
406
+ const tr = this.view.state.tr.setNodeMarkup(pos, null, {
407
+ ...this.node.attrs,
408
+ width: '100%',
409
+ height: 'auto',
410
+ })
411
+ this.view.dispatch(tr)
412
+ return
413
+ }
414
+
354
415
  const tr = this.view.state.tr.setNodeMarkup(pos, null, { ...this.node.attrs, width: newWidth, height: newHeight })
355
416
  this.view.dispatch(tr)
356
417
  }
@@ -360,7 +421,7 @@ class ResizableImageView {
360
421
  this.node = node
361
422
  if (node.attrs.src !== this.img.src) this.img.src = node.attrs.src
362
423
  this.img.alt = node.attrs.alt || ''
363
- applyDimensionStyle(this.img, 'width', node.attrs.width)
424
+ this.applyWidthLayout(node.attrs.width)
364
425
  applyDimensionStyle(this.img, 'height', node.attrs.height)
365
426
  this.applyAlignment(node.attrs.align)
366
427
  const menuVerticalPos = node.attrs.alignMenuPosition != null ? node.attrs.alignMenuPosition : (this.options && this.options.alignMenuPosition != null ? this.options.alignMenuPosition : 'below')
@@ -477,9 +538,11 @@ export const TiptapIzzyExtensionResizableImage = Node.create({
477
538
  showAlignMenu: true,
478
539
  alignMenuPosition: 'below',
479
540
  alignMenuIcons: { left: '⟸', center: '⇔', right: '⟹', clear: 'x', preview: '🔍' },
541
+ alignMenuOrder: ['left', 'center', 'right', 'clear', 'preview', 'size50', 'size100'],
480
542
  alignMenuButtonsHide: {},
481
543
  fitToContainerSelector: '.tiptap',
482
544
  autoFitWhenOversized: true,
545
+ maxAllowedWidth: null,
483
546
  }
484
547
  },
485
548
 
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.14",
4
4
  "description": "Resizable image NodeView for TipTap v2 with alignment menu and defaults",
5
5
  "license": "MIT",
6
6
  "type": "module",