{"version":3,"file":"taiga-ui-editor-extensions-group.mjs","sources":["../../../projects/editor/extensions/group/index.ts","../../../projects/editor/extensions/group/taiga-ui-editor-extensions-group.ts"],"sourcesContent":["import {tuiDeleteNode, tuiGetSelectedContent} from '@taiga-ui/editor/utils';\nimport {mergeAttributes, Node, type RawCommands} from '@tiptap/core';\n\ndeclare module '@tiptap/core' {\n    interface Commands<ReturnType> {\n        group: {\n            removeGroup(): ReturnType;\n            setGroup(): ReturnType;\n            setGroupHilite(color: string): ReturnType;\n        };\n    }\n}\n\ninterface ServerSideGlobal extends NodeJS.Global {\n    document: Document | undefined;\n}\n\ndeclare const globalThis: ServerSideGlobal;\n\nexport interface TuiEditorGroupOptions {\n    readonly createOnEnter: boolean;\n    readonly draggable: boolean;\n    readonly groupNodeClass: string;\n    readonly groupPointerNodeClass: string;\n    readonly nested: boolean;\n}\n\nexport const TUI_EDITOR_GROUP_DEFAULT_OPTIONS: TuiEditorGroupOptions = {\n    groupNodeClass: 'tui-group-node',\n    groupPointerNodeClass: 'tui-group-pointer',\n    nested: true,\n    draggable: true,\n    createOnEnter: false,\n};\n\nexport const tuiCreateGroupExtension = (\n    options: Partial<TuiEditorGroupOptions> = {},\n): Node => {\n    const {draggable, nested, groupNodeClass, groupPointerNodeClass, createOnEnter} = {\n        ...TUI_EDITOR_GROUP_DEFAULT_OPTIONS,\n        ...options,\n    };\n\n    return Node.create({\n        name: 'group',\n        draggable,\n        group: 'block',\n        content: nested ? 'block+' : 'block',\n\n        addAttributes() {\n            return {\n                style: {\n                    default: null,\n                    parseHTML: (element) => element.getAttribute('style'),\n                    renderHTML: (attributes) => {\n                        if (!attributes.style) {\n                            return {};\n                        }\n\n                        return {style: attributes.style};\n                    },\n                },\n            };\n        },\n\n        parseHTML() {\n            return [{tag: 'div[data-type=\"group\"]'}];\n        },\n\n        renderHTML({HTMLAttributes}) {\n            return ['div', mergeAttributes(HTMLAttributes, {'data-type': 'group'}), 0];\n        },\n\n        addNodeView() {\n            return ({HTMLAttributes, node}): any => {\n                if (globalThis.document) {\n                    const dom = document.createElement('div');\n                    const content = document.createElement('div');\n\n                    dom.classList.add(groupNodeClass);\n                    content.setAttribute('data-type', 'group');\n\n                    if (HTMLAttributes.style) {\n                        (node.attrs as any).style = HTMLAttributes.style;\n                        content.setAttribute('style', HTMLAttributes.style);\n                    }\n\n                    if (draggable) {\n                        const pointer = document.createElement('div');\n\n                        pointer.classList.add(groupPointerNodeClass);\n                        pointer.innerHTML = '';\n                        pointer.contentEditable = 'false';\n\n                        dom.append(pointer, content);\n                    } else {\n                        dom.append(content);\n                    }\n\n                    return {dom, contentDOM: content};\n                }\n\n                return null;\n            };\n        },\n\n        addCommands(): Partial<RawCommands> {\n            return {\n                setGroup:\n                    () =>\n                    ({commands, state}) => {\n                        this.editor.chain().focus().run();\n\n                        const content = tuiGetSelectedContent(state, '');\n                        const wrapped = content.trim().startsWith('<p>')\n                            ? content\n                            : `<p>${content}</p>`;\n                        const result = `<div data-type=\"group\">${wrapped}</div>`;\n\n                        return commands.insertContent(result);\n                    },\n                setGroupHilite:\n                    (color: string) =>\n                    ({editor}) => {\n                        let position = editor.state.selection.$anchor;\n\n                        /**\n                         * @note:\n                         * we can't mutate DOM directly in tiptap\n                         * find group element for update style attribute\n                         */\n                        for (let depth = position.depth; depth > 0; depth--) {\n                            position = editor.state.selection.$anchor;\n\n                            const node = position.node(depth);\n\n                            if (node.type.name === this.name) {\n                                /**\n                                 * @note:\n                                 * workaround for `Applying a mismatched transaction`\n                                 */\n                                setTimeout(() => {\n                                    editor.commands.updateAttributes(node.type, {\n                                        style: `background: ${color}`,\n                                    });\n                                });\n\n                                break;\n                            }\n                        }\n\n                        return true;\n                    },\n                removeGroup:\n                    () =>\n                    ({state, dispatch}) =>\n                        tuiDeleteNode(state, dispatch, this.name),\n            };\n        },\n\n        addKeyboardShortcuts(): Record<string, () => boolean> {\n            return createOnEnter ? {Enter: () => this.editor.commands.setGroup()} : {};\n        },\n    });\n};\n","/**\n * Generated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