Skip to content

Commit

Permalink
feat: support delete detail from contenteditable area
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Jul 17, 2024
1 parent e1763f1 commit 3a5c415
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
33 changes: 32 additions & 1 deletion projects/tui-editor/components/editor-socket/styles/details.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ summary {
align-items: center;
min-height: 3.5rem;
width: 100%;
padding: 0 1.25rem;
box-sizing: border-box;
border-radius: var(--tui-radius-l);
pointer-events: none;
outline: none;
word-break: break-all;
padding: 0 4.6875rem 0 1.25rem;

&:focus-visible {
outline: 2px solid var(--tui-link);
Expand Down Expand Up @@ -50,6 +51,36 @@ details[open] summary {
outline: none;
}

.t-details-delete {
position: absolute;
top: 1.4rem;
right: 2.7rem;
height: 1.125rem;
width: 1.25rem;
appearance: none;
box-sizing: border-box;
padding: 0;
background: none;
cursor: pointer;
outline: none;
border: none;

&:hover::after {
color: var(--tui-text-primary);
}

&::after {
content: '\00d7';
display: inline-block;
font: inherit;
font-size: 2rem;
color: var(--tui-text-secondary);
height: 100%;
width: 100%;
line-height: 0.8rem;
}
}

details {
width: 100%;
border: 1px solid var(--tui-base-04);
Expand Down
59 changes: 36 additions & 23 deletions projects/tui-editor/extensions/details/details.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,37 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({
},

addNodeView() {
return ({node}) => {
const wrapper = document.createElement(`div`);
const details = document.createElement(`details`);
const button = document.createElement(`button`);

wrapper.className = `t-details-wrapper`;
button.className = `t-details-arrow`;

details.open = node.attrs.opened;

button.addEventListener(`click`, () => {
details.open = !details.open;
(node.attrs as unknown as Record<string, unknown>).opened = details.open;
});

wrapper.append(details, button);

return {
dom: wrapper,
contentDOM: details,
};
return ({node, editor}): any => {
if (globalThis.document) {
const wrapper = document.createElement('div');

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
const details = document.createElement('details');

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
const collapseButton = document.createElement('button');

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
const deleteButton = document.createElement('button');

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick

wrapper.className = 't-details-wrapper';

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
collapseButton.className = 't-details-arrow';

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
deleteButton.className = 't-details-delete';

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick

details.open = node.attrs.opened;

collapseButton.addEventListener('click', () => {

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
details.open = !details.open;
(node.attrs as unknown as Record<string, unknown>).opened =
details.open;
});

deleteButton.addEventListener('click', () => {

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
tuiDeleteNode(editor.state, editor.view.dispatch, this.name);
editor.commands.focus('end');

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

View workflow job for this annotation

GitHub Actions / Lint

Strings must use backtick
});

wrapper.append(details, collapseButton, deleteButton);

return {
dom: wrapper,
contentDOM: details,
};
}
};
},

Expand All @@ -98,8 +108,11 @@ export const TuiDetails = Node.create<TuiDetailsOptions>({
},
removeDetails:
() =>
({state, dispatch}) =>
tuiDeleteNode(state, dispatch, this.name),
({editor, state, dispatch}) => {
tuiDeleteNode(state, dispatch, this.name);

return editor.commands.focus('end');
},
};
},
});

0 comments on commit 3a5c415

Please sign in to comment.