Skip to content

Commit

Permalink
fix: details extension should be correct destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Aug 8, 2024
1 parent 89e9682 commit 938027b
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions projects/tui-editor/extensions/details/details.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({
},

addNodeView() {
return ({node, editor}): any => {
return ({node, getPos}): any => {
if (globalThis.document) {
const wrapper = document.createElement(`div`);
const details = document.createElement(`details`);
Expand All @@ -70,20 +70,41 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({

wrapper.className = `t-details-wrapper`;
collapseButton.className = `t-details-arrow`;
collapseButton.type = `button`;
deleteButton.className = `t-details-delete`;

deleteButton.type = `button`;
details.open = node.attrs.opened;

collapseButton.addEventListener(`click`, () => {
let openHandler = () => {

Check failure on line 78 in projects/tui-editor/extensions/details/details.extension.ts

View workflow job for this annotation

GitHub Actions / Lint

'openHandler' is never reassigned. Use 'const' instead

Check failure on line 78 in projects/tui-editor/extensions/details/details.extension.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function
details.open = !details.open;
(node.attrs as unknown as Record<string, unknown>).opened =
details.open;
});
};

collapseButton.addEventListener(`click`, openHandler);

deleteButton.addEventListener(
`click`,
e => {
collapseButton.removeEventListener(`click`, openHandler);

const from = (getPos as any)?.() ?? 0;

this.editor
.chain()
.focus()
.setTextSelection((getPos as any)?.())
.run();

const node = this.editor.state.selection.$anchor.nodeAfter;
const to = from + (node?.nodeSize ?? 0);

this.editor.commands.deleteRange({from, to});

deleteButton.addEventListener(`click`, () => {
tuiDeleteNode(editor.state, editor.view.dispatch, this.name);
editor.commands.focus(`end`);
});
e.preventDefault();
},
{capture: true, once: true},
);

wrapper.append(details, collapseButton, deleteButton);

Expand All @@ -99,8 +120,24 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({
return {
setDetails:
() =>
({commands, state}) => {
const content = tuiGetSelectedContent(state);
({commands, editor, state}) => {
let content = '';

Check failure on line 124 in projects/tui-editor/extensions/details/details.extension.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick

const pos = this.editor.state.selection.$anchor.pos;

if (!!globalThis.document) {

Check failure on line 128 in projects/tui-editor/extensions/details/details.extension.ts

View workflow job for this annotation

GitHub Actions / Lint

Redundant double negation
content =
(document.defaultView?.window
.getSelection()
?.toString()
.trim().length ?? 0) > 0
? tuiGetSelectedContent(state)
: '';

Check failure on line 135 in projects/tui-editor/extensions/details/details.extension.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick

setTimeout(() =>
editor.chain().focus().setTextSelection(pos).run(),
);
}

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

0 comments on commit 938027b

Please sign in to comment.