Skip to content

Commit

Permalink
fix: fix delete node issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 12, 2023
1 parent 9de1a58 commit 9f566b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
13 changes: 12 additions & 1 deletion web/core/lib/editor/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
}

pre {
background: #0d0d0d;
background: #011c32;
color: #fff;
font-family: 'JetBrainsMono', monospace;
padding: 0.75rem 1rem;
Expand Down Expand Up @@ -221,6 +221,17 @@
}
}

.node-inline-completion {
display: inline-block;
background: var(--violet-3);
padding: 0 8px;
border-radius: 4px;
}

.node-inline-completion svg {
display: inline-block;
}

.toolbar-menu {
box-shadow: 0 2px 2px var(--black-a7);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node, NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react";
import { findChildren, Node, NodeViewWrapper, ReactNodeViewRenderer } from '@tiptap/react';
import { Editor } from "@tiptap/core";
import React, { useEffect, useRef } from "react";
import { KeyboardIcon } from "@radix-ui/react-icons";
Expand All @@ -13,35 +13,52 @@ declare module '@tiptap/core' {
}
}

const extensionName = "inline-completion-completion";
const extensionName = "inline-completion";
export const InlineCompletion = Node.create({
name: extensionName,
group: "block",
defining: true,
isolating: true,
hasTrigger: false,
content: "text*",
addOptions() {
return {
HTMLAttributes: {
class: "inline-completion-completion",
class: "inline-completion",
},
}
},
addKeyboardShortcuts() {
return {
"Mod-\\": (): boolean => {
// @ts-ignore
this.hasTrigger = true
this.editor.commands.triggerInlineCompletion()
return true
},
"Tab": (): boolean => {
// @ts-ignore
this.hasTrigger = false
this.editor.commands.completeInlineCompletion()
return true
},
"`": (): boolean => {
// @ts-ignore
if (!this.hasTrigger) {
return false
}
// @ts-ignore
this.hasTrigger = false
this.editor.commands.completeInlineCompletion()
return true
},
Escape: (): boolean => {
// @ts-ignore
if (!this.hasTrigger) {
return false
}
// @ts-ignore
this.hasTrigger = false
this.editor.commands.cancelInlineCompletion();
return true
},
Expand All @@ -56,10 +73,21 @@ export const InlineCompletion = Node.create({
attrs: options,
})
},
completeInlineCompletion: (options: any) => ({ commands }: { commands: any }) => {
completeInlineCompletion: (options: any) => ({ commands, tr }: { commands: any, tr: any }) => {
const pos = this.editor.view.state.selection.$anchor.pos;
commands.deleteNode(this.name)
// commands.deleteNode(this.name)
commands.insertContentAt(pos, "done completion")

try {
tr.doc.descendants((node, pos) => {
if (node.type.name == this.name) {
commands.deleteRange({ from: pos, to: pos + node.nodeSize })
return false;
}
})
} catch (e) {
console.log(e)
}
},
cancelInlineCompletion: (options: any) => ({ commands }: { commands: any }) => {
commands.deleteNode(this.name)
Expand Down Expand Up @@ -91,7 +119,7 @@ const InlineCompletionView = (props?: { editor: Editor }) => {

return (
<NodeViewWrapper ref={$container}>
<span>show inline completion <span className={'inline-completion-tip'}><KeyboardIcon/>`</span></span>
<span><KeyboardIcon/> type <span className={'inline-completion-tip'}>`</span> to completion</span>
</NodeViewWrapper>
);
};
Expand Down
2 changes: 1 addition & 1 deletion web/studio/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ input {
}

.inline-completion-tip {
margin-left: 1em;
margin-left: 0.2em;
background: #fff;
font-weight: 500;
border-radius: 0.2em;
Expand Down

0 comments on commit 9f566b3

Please sign in to comment.