Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix command plugin [WIP]

Tomas Valenta8ea924b9b806
diff --git a/components/Editor/extensions/SlashCommand/index.tsx b/components/Editor/extensions/SlashCommand/index.tsx
index 798834dbc..a8342cee7 100644
--- a/components/Editor/extensions/SlashCommand/index.tsx
+++ b/components/Editor/extensions/SlashCommand/index.tsx
@@ -6,7 +6,6 @@ import {
} from 'react'
import { ChainedCommands } from '@tiptap/core'
-
import { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'
import Item from './Item'
@@ -80,7 +79,7 @@ const SlashCommand: CommandList = forwardRef(({
flex
flex-col
- flex-shrink-0
+ shrink-0
overflow-y-auto
overscroll-none
diff --git a/editor/extensions/command/suggestion.ts b/editor/extensions/command/suggestion.ts
index fcdfcd8d8..d95109553 100644
--- a/editor/extensions/command/suggestion.ts
+++ b/editor/extensions/command/suggestion.ts
@@ -60,7 +60,16 @@ export function Suggestion<Item = unknown>({
const allowSpaces = false
- return new Plugin({
+ return new Plugin<{
+ active: boolean
+ range?: {
+ from: number
+ to: number
+ }
+ query: string | null
+ text: string | null
+ wasCharTyped: boolean
+ }>({
key: new PluginKey('suggestion'),
view() {
@@ -130,7 +139,6 @@ export function Suggestion<Item = unknown>({
init() {
return {
active: false,
- range: {},
query: null,
text: null,
wasCharTyped: false,
@@ -190,6 +198,8 @@ export function Suggestion<Item = unknown>({
props: {
handleTextInput(view, from, to, text) {
const state = this.getState(view.state)
+ if (!state) return
+
const preceedingChar = view.state.doc.textBetween(from - 1, from)
if (text === char && (preceedingChar === ' ' || !preceedingChar)) {
@@ -200,7 +210,10 @@ export function Suggestion<Item = unknown>({
},
// Call the keydown hook if suggestion is active.
handleKeyDown(view, event) {
- const { active, range } = this.getState(view.state)
+ const state = this.getState(view.state)
+ if (!state) return
+
+ const { active, range } = state
if (!active) {
return false
diff --git a/editor/extensions/context/contextSchema.ts b/editor/extensions/context/contextSchema.ts
index 411af2188..619bbfcf8 100644
--- a/editor/extensions/context/contextSchema.ts
+++ b/editor/extensions/context/contextSchema.ts
@@ -41,166 +41,166 @@ export default Node.create({
})
-// import { mergeAttributes, Node } from '@tiptap/core'
-// import { Node as ProseMirrorNode } from '@tiptap/pm/model'
-// import { PluginKey } from '@tiptap/pm/state'
-// import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'
-
-// export type MentionOptions = {
-// HTMLAttributes: Record<string, any>
-// renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string
-// suggestion: Omit<SuggestionOptions, 'editor'>
-// }
-
-// export const MentionPluginKey = new PluginKey('mention')
-
-// export const Mention = Node.create<MentionOptions>({
-// name: 'mention',
-
-// addOptions() {
-// return {
-// HTMLAttributes: {},
-// renderLabel({ options, node }) {
-// return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
-// },
-// suggestion: {
-// char: '@',
-// pluginKey: MentionPluginKey,
-// command: ({ editor, range, props }) => {
-// // increase range.to by one when the next node is of type "text"
-// // and starts with a space character
-// const nodeAfter = editor.view.state.selection.$to.nodeAfter
-// const overrideSpace = nodeAfter?.text?.startsWith(' ')
-
-// if (overrideSpace) {
-// range.to += 1
-// }
-
-// editor
-// .chain()
-// .focus()
-// .insertContentAt(range, [
-// {
-// type: this.name,
-// attrs: props,
-// },
-// {
-// type: 'text',
-// text: ' ',
-// },
-// ])
-// .run()
-
-// window.getSelection()?.collapseToEnd()
-// },
-// allow: ({ state, range }) => {
-// const $from = state.doc.resolve(range.from)
-// const type = state.schema.nodes[this.name]
-// const allow = !!$from.parent.type.contentMatch.matchType(type)
-
-// return allow
-// },
-// },
-// }
-// },
-
-// group: 'inline',
-
-// inline: true,
-
-// selectable: false,
-
-// atom: true,
-
-// addAttributes() {
-// return {
-// id: {
-// default: null,
-// parseHTML: element => element.getAttribute('data-id'),
-// renderHTML: attributes => {
-// if (!attributes.id) {
-// return {}
-// }
-
-// return {
-// 'data-id': attributes.id,
-// }
-// },
-// },
-
-// label: {
-// default: null,
-// parseHTML: element => element.getAttribute('data-label'),
-// renderHTML: attributes => {
-// if (!attributes.label) {
-// return {}
-// }
-
-// return {
-// 'data-label': attributes.label,
-// }
-// },
-// },
-// }
-// },
-
-// parseHTML() {
-// return [
-// {
-// tag: `span[data-type="${this.name}"]`,
-// },
-// ]
-// },
-
-// renderHTML({ node, HTMLAttributes }) {
-// return [
-// 'span',
-// mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
-// this.options.renderLabel({
-// options: this.options,
-// node,
-// }),
-// ]
-// },
-
-// renderText({ node }) {
-// return this.options.renderLabel({
-// options: this.options,
-// node,
-// })
-// },
-
-// addKeyboardShortcuts() {
-// return {
-// Backspace: () => this.editor.commands.command(({ tr, state }) => {
-// let isMention = false
-// const { selection } = state
-// const { empty, anchor } = selection
-
-// if (!empty) {
-// return false
-// }
-
-// state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
-// if (node.type.name === this.name) {
-// isMention = true
-// tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)
-
-// return false
-// }
-// })
-
-// return isMention
-// }),
-// }
-// },
-
-// addProseMirrorPlugins() {
-// return [
-// Suggestion({
-// editor: this.editor,
-// ...this.options.suggestion,
-// }),
-// ]
-// },
-// })
\ No newline at end of file
+import { mergeAttributes, Node } from '@tiptap/core'
+import { Node as ProseMirrorNode } from '@tiptap/pm/model'
+import { PluginKey } from '@tiptap/pm/state'
+import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'
+
+export type MentionOptions = {
+ HTMLAttributes: Record<string, any>
+ renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string
+ suggestion: Omit<SuggestionOptions, 'editor'>
+}
+
+export const MentionPluginKey = new PluginKey('mention')
+
+export const Mention = Node.create<MentionOptions>({
+ name: 'mention',
+
+ addOptions() {
+ return {
+ HTMLAttributes: {},
+ renderLabel({ options, node }) {
+ return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
+ },
+ suggestion: {
+ char: '@',
+ pluginKey: MentionPluginKey,
+ command: ({ editor, range, props }) => {
+ // increase range.to by one when the next node is of type "text"
+ // and starts with a space character
+ const nodeAfter = editor.view.state.selection.$to.nodeAfter
+ const overrideSpace = nodeAfter?.text?.startsWith(' ')
+
+ if (overrideSpace) {
+ range.to += 1
+ }
+
+ editor
+ .chain()
+ .focus()
+ .insertContentAt(range, [
+ {
+ type: this.name,
+ attrs: props,
+ },
+ {
+ type: 'text',
+ text: ' ',
+ },
+ ])
+ .run()
+
+ window.getSelection()?.collapseToEnd()
+ },
+ allow: ({ state, range }) => {
+ const $from = state.doc.resolve(range.from)
+ const type = state.schema.nodes[this.name]
+ const allow = !!$from.parent.type.contentMatch.matchType(type)
+
+ return allow
+ },
+ },
+ }
+ },
+
+ group: 'inline',
+
+ inline: true,
+
+ selectable: false,
+
+ atom: true,
+
+ addAttributes() {
+ return {
+ id: {
+ default: null,
+ parseHTML: element => element.getAttribute('data-id'),
+ renderHTML: attributes => {
+ if (!attributes.id) {
+ return {}
+ }
+
+ return {
+ 'data-id': attributes.id,
+ }
+ },
+ },
+
+ label: {
+ default: null,
+ parseHTML: element => element.getAttribute('data-label'),
+ renderHTML: attributes => {
+ if (!attributes.label) {
+ return {}
+ }
+
+ return {
+ 'data-label': attributes.label,
+ }
+ },
+ },
+ }
+ },
+
+ parseHTML() {
+ return [
+ {
+ tag: `span[data-type="${this.name}"]`,
+ },
+ ]
+ },
+
+ renderHTML({ node, HTMLAttributes }) {
+ return [
+ 'span',
+ mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
+ this.options.renderLabel({
+ options: this.options,
+ node,
+ }),
+ ]
+ },
+
+ renderText({ node }) {
+ return this.options.renderLabel({
+ options: this.options,
+ node,
+ })
+ },
+
+ addKeyboardShortcuts() {
+ return {
+ Backspace: () => this.editor.commands.command(({ tr, state }) => {
+ let isMention = false
+ const { selection } = state
+ const { empty, anchor } = selection
+
+ if (!empty) {
+ return false
+ }
+
+ state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
+ if (node.type.name === this.name) {
+ isMention = true
+ tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)
+
+ return false
+ }
+ })
+
+ return isMention
+ }),
+ }
+ },
+
+ addProseMirrorPlugins() {
+ return [
+ Suggestion({
+ editor: this.editor,
+ ...this.options.suggestion,
+ }),
+ ]
+ },
+})
\ No newline at end of file