Skip to content

Commit

Permalink
refactor: make two editor working togther
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 22, 2023
1 parent b7a5da6 commit 3657bfa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
37 changes: 33 additions & 4 deletions components/editor/intelli/ai-block-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const AiBlockEditor = ({ content, cancel }) => {
const ActionBar = Extension.create({
addCommands: () => ({
callAi: () => ({ commands }) => {
console.log('call ai')
commands.insertContent('Hello World!')
},
cancelAi: () => ({ commands }) => {
Expand All @@ -20,7 +19,11 @@ export const AiBlockEditor = ({ content, cancel }) => {
this.editor.commands.callAi()
this.editor.view?.focus()
},
Escape: () => this.editor.commands.cancelAi(),
'Escape': () => {
console.log("Escape KEYBOARD??")
this.editor.commands.cancelAi()
// this.editor.view?.focus()
},
}
},
})
Expand All @@ -34,15 +37,41 @@ export const AiBlockEditor = ({ content, cancel }) => {
content: content,
editorProps: {
attributes: {
class: 'prose',
class: 'prose ai-block-editor-inner',
},
},
})

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

useEffect(() => {
const keyDownHandler = event => {
if (event.key === 'Enter') {
event.preventDefault();
editor?.commands?.newlineInCode()
editor?.view?.focus()
}
if (event.key === 'Escape') {
console.log("sfdsfsf")
event.preventDefault();
editor?.commands?.cancelAi()
}
};

document.addEventListener('keydown', keyDownHandler);
return () => {
document.removeEventListener('keydown', keyDownHandler);
};
}, [editor]);

return (
<div className={'ai-block-editor-block'}>
<EditorContent editor={editor} className={'ai-block-editor'}/>
Expand Down
26 changes: 17 additions & 9 deletions components/editor/intelli/ai-block-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AiBlockView from './ai-block-view'
const extensionName = 'quick-command'

export const createAiBlock = () => {
const pluginKey = new PluginKey(extensionName)
let isEditInChild = false

return Node.create({
name: extensionName,
Expand All @@ -29,13 +29,12 @@ export const createAiBlock = () => {
attributes => ({ commands }) => {
return commands.toggleNode(this.name, 'paragraph', attributes)
},
}
},
addAttributes() {
return {
textContent: {
default: "",
}
enableEnter:
() => ({ commands }) => {
isEditInChild = false
commands.focus()
this.editor.setEditable(true)
},
}
},
addNodeView () {
Expand All @@ -44,6 +43,8 @@ export const createAiBlock = () => {
addKeyboardShortcuts () {
return {
'Mod-/': (state, dispatch, view) => {
isEditInChild = true
this.editor.setEditable(false)
this.editor.commands.toggleAiBlock()
},
Backspace: () => {
Expand All @@ -60,10 +61,17 @@ export const createAiBlock = () => {

return false
},
Escape: () => {
console.log("Escape")
if (isEditInChild === true) {
this.editor.setEditable(true)
this.editor.commands.focus()
}
},

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

if (!this.options.exitOnTripleEnter) {
return false
Expand Down
4 changes: 2 additions & 2 deletions components/editor/intelli/ai-block-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { AiBlockEditor } from './ai-block-editor'

const AiBlockView = ( props ) => {
const $container = useRef()
console.log(props)

return (
<NodeViewWrapper className={'shadow'} ref={$container}>
<AiBlockEditor content={''} cancel={() => {
props?.deleteNode()
props?.editor?.commands.toggleAiBlock()
props?.editor?.commands.enableEnter()
}}/>
</NodeViewWrapper>
)
Expand Down
4 changes: 4 additions & 0 deletions pages/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ button {
width: 100%;
}

.ai-block-editor-inner {
min-height: 64px;
}

.ai-block-actions {
display: flex;
justify-content: flex-end;
Expand Down

0 comments on commit 3657bfa

Please sign in to comment.