From 34c24a8e7dce4bfb15ee3b294a08679dcc58701e Mon Sep 17 00:00:00 2001 From: Abramova Elizaveta Date: Fri, 12 Jun 2026 21:24:00 +0400 Subject: [PATCH 1/2] fix(169): correct creating command in menu --- .../src/hooks/useBlockMenuActions.ts | 65 +++++++++++++++---- .../src/ui/components/BlockMenu.tsx | 10 +-- .../src/ui/components/DragHandleWrapper.tsx | 6 +- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/packages/modules.editor/src/hooks/useBlockMenuActions.ts b/packages/modules.editor/src/hooks/useBlockMenuActions.ts index ea49b144..322ba7c5 100644 --- a/packages/modules.editor/src/hooks/useBlockMenuActions.ts +++ b/packages/modules.editor/src/hooks/useBlockMenuActions.ts @@ -33,19 +33,57 @@ export const useBlockMenuActions = ( const insertImage = (src: string, alt?: string) => { if (!editor || !editor.isEditable) return; - const endPos = editor.state.doc.content.size; + const activeBlock = getCurrentBlock(editor); + + if (!activeBlock?.node) return; + + const insertPos = activeBlock.pos + activeBlock.node.nodeSize; + editor .chain() .focus() - .insertContentAt(endPos, [ - { - type: 'image', - attrs: { src, alt }, - }, - ]) + .insertContentAt(insertPos, { + type: 'image', + attrs: { src, alt }, + }) .run(); }; + const createBlock = ( + editor: Editor | null, + type: BlockTypeT, + activeBlock: ActiveBlockT | undefined, + ) => { + if (!editor || !editor.isEditable || !type || !activeBlock) return; + + const currentBlock = getCurrentBlock(editor, activeBlock); + console.log(activeBlock, currentBlock); + console.log(activeBlock?.node?.attrs['id']); + + if (!currentBlock?.node) return; + + const config = NODE_TYPES_MAP[type]; + if (!config) return; + + const insertPos = currentBlock.pos + currentBlock.node.nodeSize; + + const nodeType = editor.schema.nodes[config.type]; + if (!nodeType) return; + + const newNode = nodeType.createAndFill(config.attrs); + if (!newNode) return; + + editor.chain().focus().insertContentAt(insertPos, newNode.toJSON()).run(); + }; + + const downloadImage = (src: string) => { + const link = document.createElement('a'); + link.setAttribute('target', '_blank'); + link.href = src; + link.download = 'image.png'; + link.click(); + }; + const changeType = (type?: BlockTypeT) => { if (!editor || !editor.isEditable || !type || !getActiveBlock) return; @@ -69,15 +107,13 @@ export const useBlockMenuActions = ( }); }; - const downloadImage = (src: string) => { - const link = document.createElement('a'); - link.setAttribute('target', '_blank'); - link.href = src; - link.download = 'image.png'; - link.click(); + // В момент вызова получаем свежую позицию + const insertBlock = (type: BlockTypeT) => { + if (!getActiveBlock) return; + const activeBlock = getActiveBlock(); + return createBlock(editor, type, activeBlock); }; - // В момент вызова получаем свежую позицию const moveUp = () => { if (!getActiveBlock) return; const activeBlock = getActiveBlock(); @@ -110,6 +146,7 @@ export const useBlockMenuActions = ( downloadImage, moveDown, moveUp, + insertBlock, }; }; diff --git a/packages/modules.editor/src/ui/components/BlockMenu.tsx b/packages/modules.editor/src/ui/components/BlockMenu.tsx index 7488f496..44566fd9 100644 --- a/packages/modules.editor/src/ui/components/BlockMenu.tsx +++ b/packages/modules.editor/src/ui/components/BlockMenu.tsx @@ -43,7 +43,7 @@ export const BlockMenu = ({ }: BlockMenuPropsT) => { const isMac = navigator.platform.toUpperCase().includes('MAC'); const { openModal } = useInterfaceStore(); - const { changeType, duplicate, remove, moveUp, moveDown } = useBlockMenuActions( + const { insertBlock, duplicate, remove, moveUp, moveDown } = useBlockMenuActions( editor, getActiveBlock, ); @@ -65,22 +65,22 @@ export const BlockMenu = ({ onCloseAutoFocus={(e) => e.preventDefault()} className="border-gray-10 bg-gray-0 flex w-auto flex-col gap-1 space-y-1 rounded-lg border p-2 text-gray-100" > - changeType('paragraph')}> + insertBlock('paragraph')}> Текст - changeType('heading1')}> + insertBlock('heading1')}>

Заголовок 1 - changeType('heading2')}> + insertBlock('heading2')}>

Заголовок 2 - changeType('heading3')}> + insertBlock('heading3')}>

Заголовок 3 diff --git a/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx b/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx index ddc341f3..146653fa 100644 --- a/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx +++ b/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx @@ -26,7 +26,7 @@ export const DragHandleWrapper = ({ const handleNodeChange = useCallback((data: ActiveBlockT) => { if (!data?.node || data?.pos === null) return; - const id = data.node.attrs?.['data-uid'] ?? data.node.attrs?.id ?? null; + const id = data.node.attrs?.['id'] ?? data.node.attrs?.id ?? null; activeBlockRef.current = { pos: data.pos, id }; }, []); @@ -38,15 +38,17 @@ export const DragHandleWrapper = ({ try { const { doc } = editor.state; + console.log('in wrapper', id); // Сначала пробуем найти по id (надёжно при Yjs-синке) if (id) { let found: ActiveBlockT | undefined; doc.descendants((node, nodePos) => { if (found) return false; - const nodeId = node.attrs?.['data-uid'] ?? node.attrs?.id; + const nodeId = node.attrs?.['id'] ?? node.attrs?.id; if (nodeId === id && node.isBlock) { found = { editor, node, pos: nodePos }; + console.log(nodePos); return false; } return true; From 197016ea04fcc81b7c6e1f76d160983e19de084c Mon Sep 17 00:00:00 2001 From: Abramova Elizaveta Date: Sun, 21 Jun 2026 11:59:15 +0400 Subject: [PATCH 2/2] fix(169): delete logs --- packages/modules.editor/src/hooks/useBlockMenuActions.ts | 2 -- packages/modules.editor/src/ui/components/DragHandleWrapper.tsx | 2 -- 2 files changed, 4 deletions(-) diff --git a/packages/modules.editor/src/hooks/useBlockMenuActions.ts b/packages/modules.editor/src/hooks/useBlockMenuActions.ts index 322ba7c5..6bb0836d 100644 --- a/packages/modules.editor/src/hooks/useBlockMenuActions.ts +++ b/packages/modules.editor/src/hooks/useBlockMenuActions.ts @@ -57,8 +57,6 @@ export const useBlockMenuActions = ( if (!editor || !editor.isEditable || !type || !activeBlock) return; const currentBlock = getCurrentBlock(editor, activeBlock); - console.log(activeBlock, currentBlock); - console.log(activeBlock?.node?.attrs['id']); if (!currentBlock?.node) return; diff --git a/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx b/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx index 146653fa..bd67a6c8 100644 --- a/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx +++ b/packages/modules.editor/src/ui/components/DragHandleWrapper.tsx @@ -38,7 +38,6 @@ export const DragHandleWrapper = ({ try { const { doc } = editor.state; - console.log('in wrapper', id); // Сначала пробуем найти по id (надёжно при Yjs-синке) if (id) { @@ -48,7 +47,6 @@ export const DragHandleWrapper = ({ const nodeId = node.attrs?.['id'] ?? node.attrs?.id; if (nodeId === id && node.isBlock) { found = { editor, node, pos: nodePos }; - console.log(nodePos); return false; } return true;