Skip to content

Commit

Permalink
feat(addon-editor): add selected content into details (#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin authored Jun 3, 2022
1 parent 0c24faa commit 3c7c53b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions projects/addon-editor/extensions/details/details.extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {CommandProps, mergeAttributes, Node, RawCommands} from '@tiptap/core';
import {
CommandProps,
getHTMLFromFragment,
mergeAttributes,
Node,
RawCommands,
} from '@tiptap/core';
import {EditorState} from 'prosemirror-state';

export interface TuiDetailsOptions {
Expand Down Expand Up @@ -91,9 +97,17 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({
() =>
({commands, state}) => {
const currentNodeContent = state.selection.$head.parent.textContent;
const selected = state.doc.cut(
state.selection.from,
state.selection.to,
);

const content = selected.content.size
? getHTMLFromFragment(selected.content, state.schema)
: currentNodeContent;

return commands.insertContent(
`<details data-opened="true"><summary><p></p></summary><div data-type="details-content"><p>${currentNodeContent}</p></div></details><p></p>`,
`<details data-opened="true"><summary><p></p></summary><div data-type="details-content"><p>${content}</p></div></details><p></p>`,
);
},
removeDetails:
Expand Down

0 comments on commit 3c7c53b

Please sign in to comment.