@tiptap/core 3.9.1 → 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.js CHANGED
@@ -3521,9 +3521,13 @@ var ExtensionManager = class {
3521
3521
  if (!addNodeView) {
3522
3522
  return [];
3523
3523
  }
3524
+ const nodeViewResult = addNodeView();
3525
+ if (!nodeViewResult) {
3526
+ return [];
3527
+ }
3524
3528
  const nodeview = (node, view, getPos, decorations, innerDecorations) => {
3525
3529
  const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
3526
- return addNodeView()({
3530
+ return nodeViewResult({
3527
3531
  // pass-through
3528
3532
  node,
3529
3533
  view,
@@ -5010,6 +5014,539 @@ var h = (tag, attributes) => {
5010
5014
  return [tag, rest, children];
5011
5015
  };
5012
5016
 
5017
+ // src/lib/ResizableNodeview.ts
5018
+ var isTouchEvent = (e) => {
5019
+ return "touches" in e;
5020
+ };
5021
+ var ResizableNodeview = class {
5022
+ /**
5023
+ * Creates a new ResizableNodeview instance.
5024
+ *
5025
+ * The constructor sets up the resize handles, applies initial sizing from
5026
+ * node attributes, and configures all resize behavior options.
5027
+ *
5028
+ * @param options - Configuration options for the resizable node view
5029
+ */
5030
+ constructor(options) {
5031
+ /** Active resize handle directions */
5032
+ this.directions = ["bottom-left", "bottom-right", "top-left", "top-right"];
5033
+ /** Minimum allowed dimensions */
5034
+ this.minSize = {
5035
+ height: 8,
5036
+ width: 8
5037
+ };
5038
+ /** Whether to always preserve aspect ratio */
5039
+ this.preserveAspectRatio = false;
5040
+ /** CSS class names for elements */
5041
+ this.classNames = {
5042
+ container: "",
5043
+ wrapper: "",
5044
+ handle: "",
5045
+ resizing: ""
5046
+ };
5047
+ /** Initial width of the element (for aspect ratio calculation) */
5048
+ this.initialWidth = 0;
5049
+ /** Initial height of the element (for aspect ratio calculation) */
5050
+ this.initialHeight = 0;
5051
+ /** Calculated aspect ratio (width / height) */
5052
+ this.aspectRatio = 1;
5053
+ /** Whether a resize operation is currently active */
5054
+ this.isResizing = false;
5055
+ /** The handle currently being dragged */
5056
+ this.activeHandle = null;
5057
+ /** Starting mouse X position when resize began */
5058
+ this.startX = 0;
5059
+ /** Starting mouse Y position when resize began */
5060
+ this.startY = 0;
5061
+ /** Element width when resize began */
5062
+ this.startWidth = 0;
5063
+ /** Element height when resize began */
5064
+ this.startHeight = 0;
5065
+ /** Whether Shift key is currently pressed (for temporary aspect ratio lock) */
5066
+ this.isShiftKeyPressed = false;
5067
+ /**
5068
+ * Handles mouse movement during an active resize.
5069
+ *
5070
+ * Calculates the delta from the starting position, computes new dimensions
5071
+ * based on the active handle direction, applies constraints and aspect ratio,
5072
+ * then updates the element's style and calls the onResize callback.
5073
+ *
5074
+ * @param event - The mouse move event
5075
+ */
5076
+ this.handleMouseMove = (event) => {
5077
+ if (!this.isResizing || !this.activeHandle) {
5078
+ return;
5079
+ }
5080
+ const deltaX = event.clientX - this.startX;
5081
+ const deltaY = event.clientY - this.startY;
5082
+ this.handleResize(deltaX, deltaY);
5083
+ };
5084
+ this.handleTouchMove = (event) => {
5085
+ if (!this.isResizing || !this.activeHandle) {
5086
+ return;
5087
+ }
5088
+ const touch = event.touches[0];
5089
+ if (!touch) {
5090
+ return;
5091
+ }
5092
+ const deltaX = touch.clientX - this.startX;
5093
+ const deltaY = touch.clientY - this.startY;
5094
+ this.handleResize(deltaX, deltaY);
5095
+ };
5096
+ /**
5097
+ * Completes the resize operation when the mouse button is released.
5098
+ *
5099
+ * Captures final dimensions, calls the onCommit callback to persist changes,
5100
+ * removes the resizing state and class, and cleans up document-level listeners.
5101
+ */
5102
+ this.handleMouseUp = () => {
5103
+ if (!this.isResizing) {
5104
+ return;
5105
+ }
5106
+ const finalWidth = this.element.offsetWidth;
5107
+ const finalHeight = this.element.offsetHeight;
5108
+ this.onCommit(finalWidth, finalHeight);
5109
+ this.isResizing = false;
5110
+ this.activeHandle = null;
5111
+ this.container.dataset.resizeState = "false";
5112
+ if (this.classNames.resizing) {
5113
+ this.container.classList.remove(this.classNames.resizing);
5114
+ }
5115
+ document.removeEventListener("mousemove", this.handleMouseMove);
5116
+ document.removeEventListener("mouseup", this.handleMouseUp);
5117
+ document.removeEventListener("keydown", this.handleKeyDown);
5118
+ document.removeEventListener("keyup", this.handleKeyUp);
5119
+ };
5120
+ /**
5121
+ * Tracks Shift key state to enable temporary aspect ratio locking.
5122
+ *
5123
+ * When Shift is pressed during resize, aspect ratio is preserved even if
5124
+ * preserveAspectRatio is false.
5125
+ *
5126
+ * @param event - The keyboard event
5127
+ */
5128
+ this.handleKeyDown = (event) => {
5129
+ if (event.key === "Shift") {
5130
+ this.isShiftKeyPressed = true;
5131
+ }
5132
+ };
5133
+ /**
5134
+ * Tracks Shift key release to disable temporary aspect ratio locking.
5135
+ *
5136
+ * @param event - The keyboard event
5137
+ */
5138
+ this.handleKeyUp = (event) => {
5139
+ if (event.key === "Shift") {
5140
+ this.isShiftKeyPressed = false;
5141
+ }
5142
+ };
5143
+ var _a, _b, _c, _d, _e;
5144
+ this.node = options.node;
5145
+ this.element = options.element;
5146
+ this.contentElement = options.contentElement;
5147
+ this.getPos = options.getPos;
5148
+ this.onResize = options.onResize;
5149
+ this.onCommit = options.onCommit;
5150
+ this.onUpdate = options.onUpdate;
5151
+ if ((_a = options.options) == null ? void 0 : _a.min) {
5152
+ this.minSize = {
5153
+ ...this.minSize,
5154
+ ...options.options.min
5155
+ };
5156
+ }
5157
+ if ((_b = options.options) == null ? void 0 : _b.max) {
5158
+ this.maxSize = options.options.max;
5159
+ }
5160
+ if ((_c = options == null ? void 0 : options.options) == null ? void 0 : _c.directions) {
5161
+ this.directions = options.options.directions;
5162
+ }
5163
+ if ((_d = options.options) == null ? void 0 : _d.preserveAspectRatio) {
5164
+ this.preserveAspectRatio = options.options.preserveAspectRatio;
5165
+ }
5166
+ if ((_e = options.options) == null ? void 0 : _e.className) {
5167
+ this.classNames = {
5168
+ container: options.options.className.container || "",
5169
+ wrapper: options.options.className.wrapper || "",
5170
+ handle: options.options.className.handle || "",
5171
+ resizing: options.options.className.resizing || ""
5172
+ };
5173
+ }
5174
+ this.wrapper = this.createWrapper();
5175
+ this.container = this.createContainer();
5176
+ this.applyInitialSize();
5177
+ this.attachHandles();
5178
+ }
5179
+ /**
5180
+ * Returns the top-level DOM node that should be placed in the editor.
5181
+ *
5182
+ * This is required by the ProseMirror NodeView interface. The container
5183
+ * includes the wrapper, handles, and the actual content element.
5184
+ *
5185
+ * @returns The container element to be inserted into the editor
5186
+ */
5187
+ get dom() {
5188
+ return this.container;
5189
+ }
5190
+ get contentDOM() {
5191
+ return this.contentElement;
5192
+ }
5193
+ /**
5194
+ * Called when the node's content or attributes change.
5195
+ *
5196
+ * Updates the internal node reference. If a custom `onUpdate` callback
5197
+ * was provided, it will be called to handle additional update logic.
5198
+ *
5199
+ * @param node - The new/updated node
5200
+ * @param decorations - Node decorations
5201
+ * @param innerDecorations - Inner decorations
5202
+ * @returns `false` if the node type has changed (requires full rebuild), otherwise the result of `onUpdate` or `true`
5203
+ */
5204
+ update(node, decorations, innerDecorations) {
5205
+ if (node.type !== this.node.type) {
5206
+ return false;
5207
+ }
5208
+ this.node = node;
5209
+ if (this.onUpdate) {
5210
+ return this.onUpdate(node, decorations, innerDecorations);
5211
+ }
5212
+ return true;
5213
+ }
5214
+ /**
5215
+ * Cleanup method called when the node view is being removed.
5216
+ *
5217
+ * Removes all event listeners to prevent memory leaks. This is required
5218
+ * by the ProseMirror NodeView interface. If a resize is active when
5219
+ * destroy is called, it will be properly cancelled.
5220
+ */
5221
+ destroy() {
5222
+ if (this.isResizing) {
5223
+ this.container.dataset.resizeState = "false";
5224
+ if (this.classNames.resizing) {
5225
+ this.container.classList.remove(this.classNames.resizing);
5226
+ }
5227
+ document.removeEventListener("mousemove", this.handleMouseMove);
5228
+ document.removeEventListener("mouseup", this.handleMouseUp);
5229
+ document.removeEventListener("keydown", this.handleKeyDown);
5230
+ document.removeEventListener("keyup", this.handleKeyUp);
5231
+ this.isResizing = false;
5232
+ this.activeHandle = null;
5233
+ }
5234
+ this.container.remove();
5235
+ }
5236
+ /**
5237
+ * Creates the outer container element.
5238
+ *
5239
+ * The container is the top-level element returned by the NodeView and
5240
+ * wraps the entire resizable node. It's set up with flexbox to handle
5241
+ * alignment and includes data attributes for styling and identification.
5242
+ *
5243
+ * @returns The container element
5244
+ */
5245
+ createContainer() {
5246
+ const element = document.createElement("div");
5247
+ element.dataset.resizeContainer = "";
5248
+ element.dataset.node = this.node.type.name;
5249
+ element.style.display = "flex";
5250
+ element.style.justifyContent = "flex-start";
5251
+ element.style.alignItems = "flex-start";
5252
+ if (this.classNames.container) {
5253
+ element.className = this.classNames.container;
5254
+ }
5255
+ element.appendChild(this.wrapper);
5256
+ return element;
5257
+ }
5258
+ /**
5259
+ * Creates the wrapper element that contains the content and handles.
5260
+ *
5261
+ * The wrapper uses relative positioning so that resize handles can be
5262
+ * positioned absolutely within it. This is the direct parent of the
5263
+ * content element being made resizable.
5264
+ *
5265
+ * @returns The wrapper element
5266
+ */
5267
+ createWrapper() {
5268
+ const element = document.createElement("div");
5269
+ element.style.position = "relative";
5270
+ element.style.display = "block";
5271
+ element.dataset.resizeWrapper = "";
5272
+ if (this.classNames.wrapper) {
5273
+ element.className = this.classNames.wrapper;
5274
+ }
5275
+ element.appendChild(this.element);
5276
+ return element;
5277
+ }
5278
+ /**
5279
+ * Creates a resize handle element for a specific direction.
5280
+ *
5281
+ * Each handle is absolutely positioned and includes a data attribute
5282
+ * identifying its direction for styling purposes.
5283
+ *
5284
+ * @param direction - The resize direction for this handle
5285
+ * @returns The handle element
5286
+ */
5287
+ createHandle(direction) {
5288
+ const handle = document.createElement("div");
5289
+ handle.dataset.resizeHandle = direction;
5290
+ handle.style.position = "absolute";
5291
+ if (this.classNames.handle) {
5292
+ handle.className = this.classNames.handle;
5293
+ }
5294
+ return handle;
5295
+ }
5296
+ /**
5297
+ * Positions a handle element according to its direction.
5298
+ *
5299
+ * Corner handles (e.g., 'top-left') are positioned at the intersection
5300
+ * of two edges. Edge handles (e.g., 'top') span the full width or height.
5301
+ *
5302
+ * @param handle - The handle element to position
5303
+ * @param direction - The direction determining the position
5304
+ */
5305
+ positionHandle(handle, direction) {
5306
+ const isTop = direction.includes("top");
5307
+ const isBottom = direction.includes("bottom");
5308
+ const isLeft = direction.includes("left");
5309
+ const isRight = direction.includes("right");
5310
+ if (isTop) {
5311
+ handle.style.top = "0";
5312
+ }
5313
+ if (isBottom) {
5314
+ handle.style.bottom = "0";
5315
+ }
5316
+ if (isLeft) {
5317
+ handle.style.left = "0";
5318
+ }
5319
+ if (isRight) {
5320
+ handle.style.right = "0";
5321
+ }
5322
+ if (direction === "top" || direction === "bottom") {
5323
+ handle.style.left = "0";
5324
+ handle.style.right = "0";
5325
+ }
5326
+ if (direction === "left" || direction === "right") {
5327
+ handle.style.top = "0";
5328
+ handle.style.bottom = "0";
5329
+ }
5330
+ }
5331
+ /**
5332
+ * Creates and attaches all resize handles to the wrapper.
5333
+ *
5334
+ * Iterates through the configured directions, creates a handle for each,
5335
+ * positions it, attaches the mousedown listener, and appends it to the DOM.
5336
+ */
5337
+ attachHandles() {
5338
+ this.directions.forEach((direction) => {
5339
+ const handle = this.createHandle(direction);
5340
+ this.positionHandle(handle, direction);
5341
+ handle.addEventListener("mousedown", (event) => this.handleResizeStart(event, direction));
5342
+ handle.addEventListener("touchstart", (event) => this.handleResizeStart(event, direction));
5343
+ this.wrapper.appendChild(handle);
5344
+ });
5345
+ }
5346
+ /**
5347
+ * Applies initial sizing from node attributes to the element.
5348
+ *
5349
+ * If width/height attributes exist on the node, they're applied to the element.
5350
+ * Otherwise, the element's natural/current dimensions are measured. The aspect
5351
+ * ratio is calculated for later use in aspect-ratio-preserving resizes.
5352
+ */
5353
+ applyInitialSize() {
5354
+ const width = this.node.attrs.width;
5355
+ const height = this.node.attrs.height;
5356
+ if (width) {
5357
+ this.element.style.width = `${width}px`;
5358
+ this.initialWidth = width;
5359
+ } else {
5360
+ this.initialWidth = this.element.offsetWidth;
5361
+ }
5362
+ if (height) {
5363
+ this.element.style.height = `${height}px`;
5364
+ this.initialHeight = height;
5365
+ } else {
5366
+ this.initialHeight = this.element.offsetHeight;
5367
+ }
5368
+ if (this.initialWidth > 0 && this.initialHeight > 0) {
5369
+ this.aspectRatio = this.initialWidth / this.initialHeight;
5370
+ }
5371
+ }
5372
+ /**
5373
+ * Initiates a resize operation when a handle is clicked.
5374
+ *
5375
+ * Captures the starting mouse position and element dimensions, sets up
5376
+ * the resize state, adds the resizing class and state attribute, and
5377
+ * attaches document-level listeners for mouse movement and keyboard input.
5378
+ *
5379
+ * @param event - The mouse down event
5380
+ * @param direction - The direction of the handle being dragged
5381
+ */
5382
+ handleResizeStart(event, direction) {
5383
+ event.preventDefault();
5384
+ event.stopPropagation();
5385
+ this.isResizing = true;
5386
+ this.activeHandle = direction;
5387
+ if (isTouchEvent(event)) {
5388
+ this.startX = event.touches[0].clientX;
5389
+ this.startY = event.touches[0].clientY;
5390
+ } else {
5391
+ this.startX = event.clientX;
5392
+ this.startY = event.clientY;
5393
+ }
5394
+ this.startWidth = this.element.offsetWidth;
5395
+ this.startHeight = this.element.offsetHeight;
5396
+ if (this.startWidth > 0 && this.startHeight > 0) {
5397
+ this.aspectRatio = this.startWidth / this.startHeight;
5398
+ }
5399
+ const pos = this.getPos();
5400
+ if (pos !== void 0) {
5401
+ }
5402
+ this.container.dataset.resizeState = "true";
5403
+ if (this.classNames.resizing) {
5404
+ this.container.classList.add(this.classNames.resizing);
5405
+ }
5406
+ document.addEventListener("mousemove", this.handleMouseMove);
5407
+ document.addEventListener("touchmove", this.handleTouchMove);
5408
+ document.addEventListener("mouseup", this.handleMouseUp);
5409
+ document.addEventListener("keydown", this.handleKeyDown);
5410
+ document.addEventListener("keyup", this.handleKeyUp);
5411
+ }
5412
+ handleResize(deltaX, deltaY) {
5413
+ if (!this.activeHandle) {
5414
+ return;
5415
+ }
5416
+ const shouldPreserveAspectRatio = this.preserveAspectRatio || this.isShiftKeyPressed;
5417
+ const { width, height } = this.calculateNewDimensions(this.activeHandle, deltaX, deltaY);
5418
+ const constrained = this.applyConstraints(width, height, shouldPreserveAspectRatio);
5419
+ this.element.style.width = `${constrained.width}px`;
5420
+ this.element.style.height = `${constrained.height}px`;
5421
+ if (this.onResize) {
5422
+ this.onResize(constrained.width, constrained.height);
5423
+ }
5424
+ }
5425
+ /**
5426
+ * Calculates new dimensions based on mouse delta and resize direction.
5427
+ *
5428
+ * Takes the starting dimensions and applies the mouse movement delta
5429
+ * according to the handle direction. For corner handles, both dimensions
5430
+ * are affected. For edge handles, only one dimension changes. If aspect
5431
+ * ratio should be preserved, delegates to applyAspectRatio.
5432
+ *
5433
+ * @param direction - The active resize handle direction
5434
+ * @param deltaX - Horizontal mouse movement since resize start
5435
+ * @param deltaY - Vertical mouse movement since resize start
5436
+ * @returns The calculated width and height
5437
+ */
5438
+ calculateNewDimensions(direction, deltaX, deltaY) {
5439
+ let newWidth = this.startWidth;
5440
+ let newHeight = this.startHeight;
5441
+ const isRight = direction.includes("right");
5442
+ const isLeft = direction.includes("left");
5443
+ const isBottom = direction.includes("bottom");
5444
+ const isTop = direction.includes("top");
5445
+ if (isRight) {
5446
+ newWidth = this.startWidth + deltaX;
5447
+ } else if (isLeft) {
5448
+ newWidth = this.startWidth - deltaX;
5449
+ }
5450
+ if (isBottom) {
5451
+ newHeight = this.startHeight + deltaY;
5452
+ } else if (isTop) {
5453
+ newHeight = this.startHeight - deltaY;
5454
+ }
5455
+ if (direction === "right" || direction === "left") {
5456
+ newWidth = this.startWidth + (isRight ? deltaX : -deltaX);
5457
+ }
5458
+ if (direction === "top" || direction === "bottom") {
5459
+ newHeight = this.startHeight + (isBottom ? deltaY : -deltaY);
5460
+ }
5461
+ const shouldPreserveAspectRatio = this.preserveAspectRatio || this.isShiftKeyPressed;
5462
+ if (shouldPreserveAspectRatio) {
5463
+ return this.applyAspectRatio(newWidth, newHeight, direction);
5464
+ }
5465
+ return { width: newWidth, height: newHeight };
5466
+ }
5467
+ /**
5468
+ * Applies min/max constraints to dimensions.
5469
+ *
5470
+ * When aspect ratio is NOT preserved, constraints are applied independently
5471
+ * to width and height. When aspect ratio IS preserved, constraints are
5472
+ * applied while maintaining the aspect ratio—if one dimension hits a limit,
5473
+ * the other is recalculated proportionally.
5474
+ *
5475
+ * This ensures that aspect ratio is never broken when constrained.
5476
+ *
5477
+ * @param width - The unconstrained width
5478
+ * @param height - The unconstrained height
5479
+ * @param preserveAspectRatio - Whether to maintain aspect ratio while constraining
5480
+ * @returns The constrained dimensions
5481
+ */
5482
+ applyConstraints(width, height, preserveAspectRatio) {
5483
+ var _a, _b, _c, _d;
5484
+ if (!preserveAspectRatio) {
5485
+ let constrainedWidth2 = Math.max(this.minSize.width, width);
5486
+ let constrainedHeight2 = Math.max(this.minSize.height, height);
5487
+ if ((_a = this.maxSize) == null ? void 0 : _a.width) {
5488
+ constrainedWidth2 = Math.min(this.maxSize.width, constrainedWidth2);
5489
+ }
5490
+ if ((_b = this.maxSize) == null ? void 0 : _b.height) {
5491
+ constrainedHeight2 = Math.min(this.maxSize.height, constrainedHeight2);
5492
+ }
5493
+ return { width: constrainedWidth2, height: constrainedHeight2 };
5494
+ }
5495
+ let constrainedWidth = width;
5496
+ let constrainedHeight = height;
5497
+ if (constrainedWidth < this.minSize.width) {
5498
+ constrainedWidth = this.minSize.width;
5499
+ constrainedHeight = constrainedWidth / this.aspectRatio;
5500
+ }
5501
+ if (constrainedHeight < this.minSize.height) {
5502
+ constrainedHeight = this.minSize.height;
5503
+ constrainedWidth = constrainedHeight * this.aspectRatio;
5504
+ }
5505
+ if (((_c = this.maxSize) == null ? void 0 : _c.width) && constrainedWidth > this.maxSize.width) {
5506
+ constrainedWidth = this.maxSize.width;
5507
+ constrainedHeight = constrainedWidth / this.aspectRatio;
5508
+ }
5509
+ if (((_d = this.maxSize) == null ? void 0 : _d.height) && constrainedHeight > this.maxSize.height) {
5510
+ constrainedHeight = this.maxSize.height;
5511
+ constrainedWidth = constrainedHeight * this.aspectRatio;
5512
+ }
5513
+ return { width: constrainedWidth, height: constrainedHeight };
5514
+ }
5515
+ /**
5516
+ * Adjusts dimensions to maintain the original aspect ratio.
5517
+ *
5518
+ * For horizontal handles (left/right), uses width as the primary dimension
5519
+ * and calculates height from it. For vertical handles (top/bottom), uses
5520
+ * height as primary and calculates width. For corner handles, uses width
5521
+ * as the primary dimension.
5522
+ *
5523
+ * @param width - The new width
5524
+ * @param height - The new height
5525
+ * @param direction - The active resize direction
5526
+ * @returns Dimensions adjusted to preserve aspect ratio
5527
+ */
5528
+ applyAspectRatio(width, height, direction) {
5529
+ const isHorizontal = direction === "left" || direction === "right";
5530
+ const isVertical = direction === "top" || direction === "bottom";
5531
+ if (isHorizontal) {
5532
+ return {
5533
+ width,
5534
+ height: width / this.aspectRatio
5535
+ };
5536
+ }
5537
+ if (isVertical) {
5538
+ return {
5539
+ width: height * this.aspectRatio,
5540
+ height
5541
+ };
5542
+ }
5543
+ return {
5544
+ width,
5545
+ height: width / this.aspectRatio
5546
+ };
5547
+ }
5548
+ };
5549
+
5013
5550
  // src/utilities/canInsertNode.ts
5014
5551
  import { NodeSelection as NodeSelection4 } from "@tiptap/pm/state";
5015
5552
  function canInsertNode(state, nodeType) {
@@ -5960,6 +6497,7 @@ export {
5960
6497
  NodePos,
5961
6498
  NodeView,
5962
6499
  PasteRule,
6500
+ ResizableNodeview,
5963
6501
  Tracker,
5964
6502
  callOrReturn,
5965
6503
  canInsertNode,