@tiptap/core 3.9.0 → 3.10.0

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.
package/dist/index.cjs CHANGED
@@ -32,6 +32,7 @@ __export(index_exports, {
32
32
  NodePos: () => NodePos,
33
33
  NodeView: () => NodeView,
34
34
  PasteRule: () => PasteRule,
35
+ ResizableNodeview: () => ResizableNodeview,
35
36
  Tracker: () => Tracker,
36
37
  callOrReturn: () => callOrReturn,
37
38
  canInsertNode: () => canInsertNode,
@@ -3647,9 +3648,13 @@ var ExtensionManager = class {
3647
3648
  if (!addNodeView) {
3648
3649
  return [];
3649
3650
  }
3651
+ const nodeViewResult = addNodeView();
3652
+ if (!nodeViewResult) {
3653
+ return [];
3654
+ }
3650
3655
  const nodeview = (node, view, getPos, decorations, innerDecorations) => {
3651
3656
  const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
3652
- return addNodeView()({
3657
+ return nodeViewResult({
3653
3658
  // pass-through
3654
3659
  node,
3655
3660
  view,
@@ -5136,6 +5141,539 @@ var h = (tag, attributes) => {
5136
5141
  return [tag, rest, children];
5137
5142
  };
5138
5143
 
5144
+ // src/lib/ResizableNodeview.ts
5145
+ var isTouchEvent = (e) => {
5146
+ return "touches" in e;
5147
+ };
5148
+ var ResizableNodeview = class {
5149
+ /**
5150
+ * Creates a new ResizableNodeview instance.
5151
+ *
5152
+ * The constructor sets up the resize handles, applies initial sizing from
5153
+ * node attributes, and configures all resize behavior options.
5154
+ *
5155
+ * @param options - Configuration options for the resizable node view
5156
+ */
5157
+ constructor(options) {
5158
+ /** Active resize handle directions */
5159
+ this.directions = ["bottom-left", "bottom-right", "top-left", "top-right"];
5160
+ /** Minimum allowed dimensions */
5161
+ this.minSize = {
5162
+ height: 8,
5163
+ width: 8
5164
+ };
5165
+ /** Whether to always preserve aspect ratio */
5166
+ this.preserveAspectRatio = false;
5167
+ /** CSS class names for elements */
5168
+ this.classNames = {
5169
+ container: "",
5170
+ wrapper: "",
5171
+ handle: "",
5172
+ resizing: ""
5173
+ };
5174
+ /** Initial width of the element (for aspect ratio calculation) */
5175
+ this.initialWidth = 0;
5176
+ /** Initial height of the element (for aspect ratio calculation) */
5177
+ this.initialHeight = 0;
5178
+ /** Calculated aspect ratio (width / height) */
5179
+ this.aspectRatio = 1;
5180
+ /** Whether a resize operation is currently active */
5181
+ this.isResizing = false;
5182
+ /** The handle currently being dragged */
5183
+ this.activeHandle = null;
5184
+ /** Starting mouse X position when resize began */
5185
+ this.startX = 0;
5186
+ /** Starting mouse Y position when resize began */
5187
+ this.startY = 0;
5188
+ /** Element width when resize began */
5189
+ this.startWidth = 0;
5190
+ /** Element height when resize began */
5191
+ this.startHeight = 0;
5192
+ /** Whether Shift key is currently pressed (for temporary aspect ratio lock) */
5193
+ this.isShiftKeyPressed = false;
5194
+ /**
5195
+ * Handles mouse movement during an active resize.
5196
+ *
5197
+ * Calculates the delta from the starting position, computes new dimensions
5198
+ * based on the active handle direction, applies constraints and aspect ratio,
5199
+ * then updates the element's style and calls the onResize callback.
5200
+ *
5201
+ * @param event - The mouse move event
5202
+ */
5203
+ this.handleMouseMove = (event) => {
5204
+ if (!this.isResizing || !this.activeHandle) {
5205
+ return;
5206
+ }
5207
+ const deltaX = event.clientX - this.startX;
5208
+ const deltaY = event.clientY - this.startY;
5209
+ this.handleResize(deltaX, deltaY);
5210
+ };
5211
+ this.handleTouchMove = (event) => {
5212
+ if (!this.isResizing || !this.activeHandle) {
5213
+ return;
5214
+ }
5215
+ const touch = event.touches[0];
5216
+ if (!touch) {
5217
+ return;
5218
+ }
5219
+ const deltaX = touch.clientX - this.startX;
5220
+ const deltaY = touch.clientY - this.startY;
5221
+ this.handleResize(deltaX, deltaY);
5222
+ };
5223
+ /**
5224
+ * Completes the resize operation when the mouse button is released.
5225
+ *
5226
+ * Captures final dimensions, calls the onCommit callback to persist changes,
5227
+ * removes the resizing state and class, and cleans up document-level listeners.
5228
+ */
5229
+ this.handleMouseUp = () => {
5230
+ if (!this.isResizing) {
5231
+ return;
5232
+ }
5233
+ const finalWidth = this.element.offsetWidth;
5234
+ const finalHeight = this.element.offsetHeight;
5235
+ this.onCommit(finalWidth, finalHeight);
5236
+ this.isResizing = false;
5237
+ this.activeHandle = null;
5238
+ this.container.dataset.resizeState = "false";
5239
+ if (this.classNames.resizing) {
5240
+ this.container.classList.remove(this.classNames.resizing);
5241
+ }
5242
+ document.removeEventListener("mousemove", this.handleMouseMove);
5243
+ document.removeEventListener("mouseup", this.handleMouseUp);
5244
+ document.removeEventListener("keydown", this.handleKeyDown);
5245
+ document.removeEventListener("keyup", this.handleKeyUp);
5246
+ };
5247
+ /**
5248
+ * Tracks Shift key state to enable temporary aspect ratio locking.
5249
+ *
5250
+ * When Shift is pressed during resize, aspect ratio is preserved even if
5251
+ * preserveAspectRatio is false.
5252
+ *
5253
+ * @param event - The keyboard event
5254
+ */
5255
+ this.handleKeyDown = (event) => {
5256
+ if (event.key === "Shift") {
5257
+ this.isShiftKeyPressed = true;
5258
+ }
5259
+ };
5260
+ /**
5261
+ * Tracks Shift key release to disable temporary aspect ratio locking.
5262
+ *
5263
+ * @param event - The keyboard event
5264
+ */
5265
+ this.handleKeyUp = (event) => {
5266
+ if (event.key === "Shift") {
5267
+ this.isShiftKeyPressed = false;
5268
+ }
5269
+ };
5270
+ var _a, _b, _c, _d, _e;
5271
+ this.node = options.node;
5272
+ this.element = options.element;
5273
+ this.contentElement = options.contentElement;
5274
+ this.getPos = options.getPos;
5275
+ this.onResize = options.onResize;
5276
+ this.onCommit = options.onCommit;
5277
+ this.onUpdate = options.onUpdate;
5278
+ if ((_a = options.options) == null ? void 0 : _a.min) {
5279
+ this.minSize = {
5280
+ ...this.minSize,
5281
+ ...options.options.min
5282
+ };
5283
+ }
5284
+ if ((_b = options.options) == null ? void 0 : _b.max) {
5285
+ this.maxSize = options.options.max;
5286
+ }
5287
+ if ((_c = options == null ? void 0 : options.options) == null ? void 0 : _c.directions) {
5288
+ this.directions = options.options.directions;
5289
+ }
5290
+ if ((_d = options.options) == null ? void 0 : _d.preserveAspectRatio) {
5291
+ this.preserveAspectRatio = options.options.preserveAspectRatio;
5292
+ }
5293
+ if ((_e = options.options) == null ? void 0 : _e.className) {
5294
+ this.classNames = {
5295
+ container: options.options.className.container || "",
5296
+ wrapper: options.options.className.wrapper || "",
5297
+ handle: options.options.className.handle || "",
5298
+ resizing: options.options.className.resizing || ""
5299
+ };
5300
+ }
5301
+ this.wrapper = this.createWrapper();
5302
+ this.container = this.createContainer();
5303
+ this.applyInitialSize();
5304
+ this.attachHandles();
5305
+ }
5306
+ /**
5307
+ * Returns the top-level DOM node that should be placed in the editor.
5308
+ *
5309
+ * This is required by the ProseMirror NodeView interface. The container
5310
+ * includes the wrapper, handles, and the actual content element.
5311
+ *
5312
+ * @returns The container element to be inserted into the editor
5313
+ */
5314
+ get dom() {
5315
+ return this.container;
5316
+ }
5317
+ get contentDOM() {
5318
+ return this.contentElement;
5319
+ }
5320
+ /**
5321
+ * Called when the node's content or attributes change.
5322
+ *
5323
+ * Updates the internal node reference. If a custom `onUpdate` callback
5324
+ * was provided, it will be called to handle additional update logic.
5325
+ *
5326
+ * @param node - The new/updated node
5327
+ * @param decorations - Node decorations
5328
+ * @param innerDecorations - Inner decorations
5329
+ * @returns `false` if the node type has changed (requires full rebuild), otherwise the result of `onUpdate` or `true`
5330
+ */
5331
+ update(node, decorations, innerDecorations) {
5332
+ if (node.type !== this.node.type) {
5333
+ return false;
5334
+ }
5335
+ this.node = node;
5336
+ if (this.onUpdate) {
5337
+ return this.onUpdate(node, decorations, innerDecorations);
5338
+ }
5339
+ return true;
5340
+ }
5341
+ /**
5342
+ * Cleanup method called when the node view is being removed.
5343
+ *
5344
+ * Removes all event listeners to prevent memory leaks. This is required
5345
+ * by the ProseMirror NodeView interface. If a resize is active when
5346
+ * destroy is called, it will be properly cancelled.
5347
+ */
5348
+ destroy() {
5349
+ if (this.isResizing) {
5350
+ this.container.dataset.resizeState = "false";
5351
+ if (this.classNames.resizing) {
5352
+ this.container.classList.remove(this.classNames.resizing);
5353
+ }
5354
+ document.removeEventListener("mousemove", this.handleMouseMove);
5355
+ document.removeEventListener("mouseup", this.handleMouseUp);
5356
+ document.removeEventListener("keydown", this.handleKeyDown);
5357
+ document.removeEventListener("keyup", this.handleKeyUp);
5358
+ this.isResizing = false;
5359
+ this.activeHandle = null;
5360
+ }
5361
+ this.container.remove();
5362
+ }
5363
+ /**
5364
+ * Creates the outer container element.
5365
+ *
5366
+ * The container is the top-level element returned by the NodeView and
5367
+ * wraps the entire resizable node. It's set up with flexbox to handle
5368
+ * alignment and includes data attributes for styling and identification.
5369
+ *
5370
+ * @returns The container element
5371
+ */
5372
+ createContainer() {
5373
+ const element = document.createElement("div");
5374
+ element.dataset.resizeContainer = "";
5375
+ element.dataset.node = this.node.type.name;
5376
+ element.style.display = "flex";
5377
+ element.style.justifyContent = "flex-start";
5378
+ element.style.alignItems = "flex-start";
5379
+ if (this.classNames.container) {
5380
+ element.className = this.classNames.container;
5381
+ }
5382
+ element.appendChild(this.wrapper);
5383
+ return element;
5384
+ }
5385
+ /**
5386
+ * Creates the wrapper element that contains the content and handles.
5387
+ *
5388
+ * The wrapper uses relative positioning so that resize handles can be
5389
+ * positioned absolutely within it. This is the direct parent of the
5390
+ * content element being made resizable.
5391
+ *
5392
+ * @returns The wrapper element
5393
+ */
5394
+ createWrapper() {
5395
+ const element = document.createElement("div");
5396
+ element.style.position = "relative";
5397
+ element.style.display = "block";
5398
+ element.dataset.resizeWrapper = "";
5399
+ if (this.classNames.wrapper) {
5400
+ element.className = this.classNames.wrapper;
5401
+ }
5402
+ element.appendChild(this.element);
5403
+ return element;
5404
+ }
5405
+ /**
5406
+ * Creates a resize handle element for a specific direction.
5407
+ *
5408
+ * Each handle is absolutely positioned and includes a data attribute
5409
+ * identifying its direction for styling purposes.
5410
+ *
5411
+ * @param direction - The resize direction for this handle
5412
+ * @returns The handle element
5413
+ */
5414
+ createHandle(direction) {
5415
+ const handle = document.createElement("div");
5416
+ handle.dataset.resizeHandle = direction;
5417
+ handle.style.position = "absolute";
5418
+ if (this.classNames.handle) {
5419
+ handle.className = this.classNames.handle;
5420
+ }
5421
+ return handle;
5422
+ }
5423
+ /**
5424
+ * Positions a handle element according to its direction.
5425
+ *
5426
+ * Corner handles (e.g., 'top-left') are positioned at the intersection
5427
+ * of two edges. Edge handles (e.g., 'top') span the full width or height.
5428
+ *
5429
+ * @param handle - The handle element to position
5430
+ * @param direction - The direction determining the position
5431
+ */
5432
+ positionHandle(handle, direction) {
5433
+ const isTop = direction.includes("top");
5434
+ const isBottom = direction.includes("bottom");
5435
+ const isLeft = direction.includes("left");
5436
+ const isRight = direction.includes("right");
5437
+ if (isTop) {
5438
+ handle.style.top = "0";
5439
+ }
5440
+ if (isBottom) {
5441
+ handle.style.bottom = "0";
5442
+ }
5443
+ if (isLeft) {
5444
+ handle.style.left = "0";
5445
+ }
5446
+ if (isRight) {
5447
+ handle.style.right = "0";
5448
+ }
5449
+ if (direction === "top" || direction === "bottom") {
5450
+ handle.style.left = "0";
5451
+ handle.style.right = "0";
5452
+ }
5453
+ if (direction === "left" || direction === "right") {
5454
+ handle.style.top = "0";
5455
+ handle.style.bottom = "0";
5456
+ }
5457
+ }
5458
+ /**
5459
+ * Creates and attaches all resize handles to the wrapper.
5460
+ *
5461
+ * Iterates through the configured directions, creates a handle for each,
5462
+ * positions it, attaches the mousedown listener, and appends it to the DOM.
5463
+ */
5464
+ attachHandles() {
5465
+ this.directions.forEach((direction) => {
5466
+ const handle = this.createHandle(direction);
5467
+ this.positionHandle(handle, direction);
5468
+ handle.addEventListener("mousedown", (event) => this.handleResizeStart(event, direction));
5469
+ handle.addEventListener("touchstart", (event) => this.handleResizeStart(event, direction));
5470
+ this.wrapper.appendChild(handle);
5471
+ });
5472
+ }
5473
+ /**
5474
+ * Applies initial sizing from node attributes to the element.
5475
+ *
5476
+ * If width/height attributes exist on the node, they're applied to the element.
5477
+ * Otherwise, the element's natural/current dimensions are measured. The aspect
5478
+ * ratio is calculated for later use in aspect-ratio-preserving resizes.
5479
+ */
5480
+ applyInitialSize() {
5481
+ const width = this.node.attrs.width;
5482
+ const height = this.node.attrs.height;
5483
+ if (width) {
5484
+ this.element.style.width = `${width}px`;
5485
+ this.initialWidth = width;
5486
+ } else {
5487
+ this.initialWidth = this.element.offsetWidth;
5488
+ }
5489
+ if (height) {
5490
+ this.element.style.height = `${height}px`;
5491
+ this.initialHeight = height;
5492
+ } else {
5493
+ this.initialHeight = this.element.offsetHeight;
5494
+ }
5495
+ if (this.initialWidth > 0 && this.initialHeight > 0) {
5496
+ this.aspectRatio = this.initialWidth / this.initialHeight;
5497
+ }
5498
+ }
5499
+ /**
5500
+ * Initiates a resize operation when a handle is clicked.
5501
+ *
5502
+ * Captures the starting mouse position and element dimensions, sets up
5503
+ * the resize state, adds the resizing class and state attribute, and
5504
+ * attaches document-level listeners for mouse movement and keyboard input.
5505
+ *
5506
+ * @param event - The mouse down event
5507
+ * @param direction - The direction of the handle being dragged
5508
+ */
5509
+ handleResizeStart(event, direction) {
5510
+ event.preventDefault();
5511
+ event.stopPropagation();
5512
+ this.isResizing = true;
5513
+ this.activeHandle = direction;
5514
+ if (isTouchEvent(event)) {
5515
+ this.startX = event.touches[0].clientX;
5516
+ this.startY = event.touches[0].clientY;
5517
+ } else {
5518
+ this.startX = event.clientX;
5519
+ this.startY = event.clientY;
5520
+ }
5521
+ this.startWidth = this.element.offsetWidth;
5522
+ this.startHeight = this.element.offsetHeight;
5523
+ if (this.startWidth > 0 && this.startHeight > 0) {
5524
+ this.aspectRatio = this.startWidth / this.startHeight;
5525
+ }
5526
+ const pos = this.getPos();
5527
+ if (pos !== void 0) {
5528
+ }
5529
+ this.container.dataset.resizeState = "true";
5530
+ if (this.classNames.resizing) {
5531
+ this.container.classList.add(this.classNames.resizing);
5532
+ }
5533
+ document.addEventListener("mousemove", this.handleMouseMove);
5534
+ document.addEventListener("touchmove", this.handleTouchMove);
5535
+ document.addEventListener("mouseup", this.handleMouseUp);
5536
+ document.addEventListener("keydown", this.handleKeyDown);
5537
+ document.addEventListener("keyup", this.handleKeyUp);
5538
+ }
5539
+ handleResize(deltaX, deltaY) {
5540
+ if (!this.activeHandle) {
5541
+ return;
5542
+ }
5543
+ const shouldPreserveAspectRatio = this.preserveAspectRatio || this.isShiftKeyPressed;
5544
+ const { width, height } = this.calculateNewDimensions(this.activeHandle, deltaX, deltaY);
5545
+ const constrained = this.applyConstraints(width, height, shouldPreserveAspectRatio);
5546
+ this.element.style.width = `${constrained.width}px`;
5547
+ this.element.style.height = `${constrained.height}px`;
5548
+ if (this.onResize) {
5549
+ this.onResize(constrained.width, constrained.height);
5550
+ }
5551
+ }
5552
+ /**
5553
+ * Calculates new dimensions based on mouse delta and resize direction.
5554
+ *
5555
+ * Takes the starting dimensions and applies the mouse movement delta
5556
+ * according to the handle direction. For corner handles, both dimensions
5557
+ * are affected. For edge handles, only one dimension changes. If aspect
5558
+ * ratio should be preserved, delegates to applyAspectRatio.
5559
+ *
5560
+ * @param direction - The active resize handle direction
5561
+ * @param deltaX - Horizontal mouse movement since resize start
5562
+ * @param deltaY - Vertical mouse movement since resize start
5563
+ * @returns The calculated width and height
5564
+ */
5565
+ calculateNewDimensions(direction, deltaX, deltaY) {
5566
+ let newWidth = this.startWidth;
5567
+ let newHeight = this.startHeight;
5568
+ const isRight = direction.includes("right");
5569
+ const isLeft = direction.includes("left");
5570
+ const isBottom = direction.includes("bottom");
5571
+ const isTop = direction.includes("top");
5572
+ if (isRight) {
5573
+ newWidth = this.startWidth + deltaX;
5574
+ } else if (isLeft) {
5575
+ newWidth = this.startWidth - deltaX;
5576
+ }
5577
+ if (isBottom) {
5578
+ newHeight = this.startHeight + deltaY;
5579
+ } else if (isTop) {
5580
+ newHeight = this.startHeight - deltaY;
5581
+ }
5582
+ if (direction === "right" || direction === "left") {
5583
+ newWidth = this.startWidth + (isRight ? deltaX : -deltaX);
5584
+ }
5585
+ if (direction === "top" || direction === "bottom") {
5586
+ newHeight = this.startHeight + (isBottom ? deltaY : -deltaY);
5587
+ }
5588
+ const shouldPreserveAspectRatio = this.preserveAspectRatio || this.isShiftKeyPressed;
5589
+ if (shouldPreserveAspectRatio) {
5590
+ return this.applyAspectRatio(newWidth, newHeight, direction);
5591
+ }
5592
+ return { width: newWidth, height: newHeight };
5593
+ }
5594
+ /**
5595
+ * Applies min/max constraints to dimensions.
5596
+ *
5597
+ * When aspect ratio is NOT preserved, constraints are applied independently
5598
+ * to width and height. When aspect ratio IS preserved, constraints are
5599
+ * applied while maintaining the aspect ratio—if one dimension hits a limit,
5600
+ * the other is recalculated proportionally.
5601
+ *
5602
+ * This ensures that aspect ratio is never broken when constrained.
5603
+ *
5604
+ * @param width - The unconstrained width
5605
+ * @param height - The unconstrained height
5606
+ * @param preserveAspectRatio - Whether to maintain aspect ratio while constraining
5607
+ * @returns The constrained dimensions
5608
+ */
5609
+ applyConstraints(width, height, preserveAspectRatio) {
5610
+ var _a, _b, _c, _d;
5611
+ if (!preserveAspectRatio) {
5612
+ let constrainedWidth2 = Math.max(this.minSize.width, width);
5613
+ let constrainedHeight2 = Math.max(this.minSize.height, height);
5614
+ if ((_a = this.maxSize) == null ? void 0 : _a.width) {
5615
+ constrainedWidth2 = Math.min(this.maxSize.width, constrainedWidth2);
5616
+ }
5617
+ if ((_b = this.maxSize) == null ? void 0 : _b.height) {
5618
+ constrainedHeight2 = Math.min(this.maxSize.height, constrainedHeight2);
5619
+ }
5620
+ return { width: constrainedWidth2, height: constrainedHeight2 };
5621
+ }
5622
+ let constrainedWidth = width;
5623
+ let constrainedHeight = height;
5624
+ if (constrainedWidth < this.minSize.width) {
5625
+ constrainedWidth = this.minSize.width;
5626
+ constrainedHeight = constrainedWidth / this.aspectRatio;
5627
+ }
5628
+ if (constrainedHeight < this.minSize.height) {
5629
+ constrainedHeight = this.minSize.height;
5630
+ constrainedWidth = constrainedHeight * this.aspectRatio;
5631
+ }
5632
+ if (((_c = this.maxSize) == null ? void 0 : _c.width) && constrainedWidth > this.maxSize.width) {
5633
+ constrainedWidth = this.maxSize.width;
5634
+ constrainedHeight = constrainedWidth / this.aspectRatio;
5635
+ }
5636
+ if (((_d = this.maxSize) == null ? void 0 : _d.height) && constrainedHeight > this.maxSize.height) {
5637
+ constrainedHeight = this.maxSize.height;
5638
+ constrainedWidth = constrainedHeight * this.aspectRatio;
5639
+ }
5640
+ return { width: constrainedWidth, height: constrainedHeight };
5641
+ }
5642
+ /**
5643
+ * Adjusts dimensions to maintain the original aspect ratio.
5644
+ *
5645
+ * For horizontal handles (left/right), uses width as the primary dimension
5646
+ * and calculates height from it. For vertical handles (top/bottom), uses
5647
+ * height as primary and calculates width. For corner handles, uses width
5648
+ * as the primary dimension.
5649
+ *
5650
+ * @param width - The new width
5651
+ * @param height - The new height
5652
+ * @param direction - The active resize direction
5653
+ * @returns Dimensions adjusted to preserve aspect ratio
5654
+ */
5655
+ applyAspectRatio(width, height, direction) {
5656
+ const isHorizontal = direction === "left" || direction === "right";
5657
+ const isVertical = direction === "top" || direction === "bottom";
5658
+ if (isHorizontal) {
5659
+ return {
5660
+ width,
5661
+ height: width / this.aspectRatio
5662
+ };
5663
+ }
5664
+ if (isVertical) {
5665
+ return {
5666
+ width: height * this.aspectRatio,
5667
+ height
5668
+ };
5669
+ }
5670
+ return {
5671
+ width,
5672
+ height: width / this.aspectRatio
5673
+ };
5674
+ }
5675
+ };
5676
+
5139
5677
  // src/utilities/canInsertNode.ts
5140
5678
  var import_state22 = require("@tiptap/pm/state");
5141
5679
  function canInsertNode(state, nodeType) {
@@ -6087,6 +6625,7 @@ var Tracker = class {
6087
6625
  NodePos,
6088
6626
  NodeView,
6089
6627
  PasteRule,
6628
+ ResizableNodeview,
6090
6629
  Tracker,
6091
6630
  callOrReturn,
6092
6631
  canInsertNode,