import Document from '@tiptap/extension-document' import Paragraph from '@tiptap/extension-paragraph' import Text from '@tiptap/extension-text' import { afterEach, describe, expect, it, vi } from 'vite-plus/test' import { Editor } from '../Editor.js' import { Node } from '../Node.js' import { ResizableNodeView } from './ResizableNodeView.js' const InlineImage = Node.create({ name: 'inlineImage', group: 'inline', inline: true, atom: true, addAttributes() { return { src: { default: null }, width: { default: null }, height: { default: null }, } }, parseHTML() { return [{ tag: 'img[data-type="inline"]' }] }, renderHTML({ HTMLAttributes }) { return ['img', { 'data-type': 'inline', ...HTMLAttributes }] }, addNodeView() { return ({ node, editor, getPos }) => { const el = document.createElement('img') return new ResizableNodeView({ element: el, node, editor, getPos: getPos as () => number, onResize: () => {}, onCommit: () => {}, onUpdate: () => true, }) } }, }) const BlockImage = Node.create({ name: 'blockImage', group: 'block', atom: true, addAttributes() { return { src: { default: null }, width: { default: null }, height: { default: null }, } }, parseHTML() { return [{ tag: 'img[data-type="block"]' }] }, renderHTML({ HTMLAttributes }) { return ['img', { 'data-type': 'block', ...HTMLAttributes }] }, addNodeView() { return ({ node, editor, getPos }) => { const el = document.createElement('img') return new ResizableNodeView({ element: el, node, editor, getPos: getPos as () => number, onResize: () => {}, onCommit: () => {}, onUpdate: () => true, }) } }, }) describe('ResizableNodeView', () => { let editor: Editor afterEach(() => { editor?.destroy() }) it('should use inline-flex display for inline nodes', () => { editor = new Editor({ extensions: [Document, Paragraph, Text, InlineImage], content: '

',
})
const container = editor.view.dom.querySelector(
'[data-resize-container][data-node="blockImage"]',
) as HTMLElement
expect(container).not.toBeNull()
expect(container.style.display).toBe('flex')
})
it('stops creating custom resize handles after the node view is destroyed', () => {
editor = new Editor({
extensions: [Document, Paragraph, Text, BlockImage],
content: 'Text
', }) const createCustomHandle = vi.fn(() => document.createElement('span')) const nodeView = new ResizableNodeView({ element: document.createElement('img'), node: editor.schema.nodes.blockImage.create(), editor, getPos: () => undefined, onCommit: () => {}, onUpdate: () => true, options: { directions: ['bottom-right'], createCustomHandle }, }) expect(createCustomHandle).toHaveBeenCalledTimes(1) editor.setEditable(false) editor.setEditable(true) expect(createCustomHandle).toHaveBeenCalledTimes(2) nodeView.destroy() createCustomHandle.mockClear() editor.setEditable(false) editor.setEditable(true) expect(createCustomHandle).not.toHaveBeenCalled() }) })