Skip to content

Commit

Permalink
feat: add cancel event to child editor
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 22, 2023
1 parent 9df4d37 commit b7a5da6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
41 changes: 21 additions & 20 deletions components/editor/intelli/ai-block-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ import React, { useEffect } from 'react'
import StarterKit from '@tiptap/starter-kit'
import { EnterIcon } from '@radix-ui/react-icons'

const ActionBar = Extension.create({
addCommands: () => ({
callAi: () => ({ commands }) => {
console.log('call ai')
commands.insertContent({
type: 'paragraph',
content: [
{
type: 'text',
text: 'Hello World!',
},
],
})
},
cancelAi: () => ({ commands }) => {
console.log('cancel ai')
export const AiBlockEditor = ({ content, cancel }) => {
const ActionBar = Extension.create({
addCommands: () => ({
callAi: () => ({ commands }) => {
console.log('call ai')
commands.insertContent('Hello World!')
},
cancelAi: () => ({ commands }) => {
cancel()
},
}),
addKeyboardShortcuts() {
return {
'Mod-Enter': () => {
this.editor.commands.callAi()
this.editor.view?.focus()
},
Escape: () => this.editor.commands.cancelAi(),
}
},
}),
})
})

export const AiBlockEditor = ({ content }) => {
const extensions = [
StarterKit,
ActionBar,
Expand All @@ -39,7 +40,7 @@ export const AiBlockEditor = ({ content }) => {
})

useEffect(() => {
if (editor) editor.view.focus();
if (editor) editor.view.focus()
}, [editor])

return (
Expand Down
10 changes: 9 additions & 1 deletion components/editor/intelli/ai-block-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export const createAiBlock = () => {
},
}
},
addAttributes() {
return {
textContent: {
default: "",
}
}
},
addNodeView () {
return ReactNodeViewRenderer(AiBlockView)
},
Expand All @@ -48,7 +55,6 @@ export const createAiBlock = () => {
}

if (isAtStart || !$anchor.parent.textContent.length) {
console.log($anchor.parent.textContent)
return this.editor.commands.clearNodes()
}

Expand All @@ -57,6 +63,8 @@ export const createAiBlock = () => {

// exit node on triple enter
Enter: ({ editor }) => {
// if (this.attributes.isTypingInChild) return false;

if (!this.options.exitOnTripleEnter) {
return false
}
Expand Down
10 changes: 6 additions & 4 deletions components/editor/intelli/ai-block-view.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { useRef } from 'react'
import { NodeViewContent, NodeViewWrapper } from '@tiptap/react'
import { NodeViewWrapper } from '@tiptap/react'
import { AiBlockEditor } from './ai-block-editor'

const AiBlockView = ({ node: { attrs: { language: defaultLanguage } }, updateAttributes, extension }) => {
const AiBlockView = ( props ) => {
const $container = useRef()
console.log(updateAttributes)
console.log(props)

return (
<NodeViewWrapper className={'shadow'} ref={$container}>
<AiBlockEditor content={''}/>
<AiBlockEditor content={''} cancel={() => {
props?.deleteNode()
}}/>
</NodeViewWrapper>
)
}
Expand Down
1 change: 0 additions & 1 deletion components/editor/live-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Hi there, BB is editor for Unit Mesh architecture paradigms, the next-gen softwa
2. use \`Alt\` + \`/\` or \`Command\` + \`/\` to show custom input box.
3. select text and right click to see the context menu.
/continue
`
const LiveEditor = () => {
const editor = useEditor({
Expand Down

0 comments on commit b7a5da6

Please sign in to comment.