Skip to content

Commit

Permalink
feat: add handle for ai block
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 22, 2023
1 parent 836aac5 commit d293224
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 47 deletions.
91 changes: 91 additions & 0 deletions components/editor/intelli/ai-block-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* license: MIT
* author: Tiptap
* origin: https://github.com/ueberdosis/tiptap/blob/develop/packages/extension-code-block/src/code-block.ts
*/
import { Node, ReactNodeViewRenderer } from '@tiptap/react'
import { PluginKey } from '@tiptap/pm/state'

import { AiBlockView } from './ai-block-view'

const extensionName = 'quick-command'

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

return Node.create({
name: extensionName,
group: 'block',
defining: true,
content: 'text*',

addCommands () {
return {
setAiBlock:
attributes => ({ commands }) => {
return commands.setNode(this.name, attributes)
},
toggleAiBlock:
attributes => ({ commands }) => {
return commands.toggleNode(this.name, 'paragraph', attributes)
},
}
},
addNodeView () {
return ReactNodeViewRenderer(AiBlockView)
},
addKeyboardShortcuts () {
return {
'Mod-/': (state, dispatch, view) => {
this.editor.commands.toggleAiBlock()
},
Backspace: () => {
const { empty, $anchor } = this.editor.state.selection
const isAtStart = $anchor.pos === 1

if (!empty || $anchor.parent.type.name !== this.name) {
return false
}

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

return false
},

// exit node on triple enter
Enter: ({ editor }) => {
if (!this.options.exitOnTripleEnter) {
return false
}

const { state } = editor
const { selection } = state
const { $from, empty } = selection

if (!empty || $from.parent.type !== this.type) {
return false
}

const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2
const endsWithDoubleNewline = $from.parent.textContent.endsWith('\n\n')

if (!isAtEnd || !endsWithDoubleNewline) {
return false
}

return editor
.chain()
.command(({ tr }) => {
tr.delete($from.pos - 2, $from.pos)

return true
})
.exitCode()
.run()
},
}
},
})
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { NodeViewWrapper } from '@tiptap/react'

export class QuickView extends React.Component {
export class AiBlockView extends React.Component {
constructor (props) {
super(props)
this.$container = React.createRef()
Expand Down
45 changes: 0 additions & 45 deletions components/editor/intelli/ai-block.js

This file was deleted.

2 changes: 1 addition & 1 deletion components/editor/live-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MarkdownIt from 'markdown-it'
import { MenuBubble } from './intelli/menu/menu-bubble'
import { createSlashExtension } from './intelli/slash-extension'
import { CommandFunctions } from './action/command-functions'
import { createAiBlock } from './intelli/ai-block'
import { createAiBlock } from './intelli/ai-block-extension'

const md = new MarkdownIt()

Expand Down

0 comments on commit d293224

Please sign in to comment.