Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Create inline context command items
Tomas Valenta94ce4a0d27db
diff --git a/components/Editor/extensions/SlashCommand/index.tsx b/components/Editor/extensions/SlashCommand/index.tsxindex a8342cee7..f02305b92 100644--- a/components/Editor/extensions/SlashCommand/index.tsx+++ b/components/Editor/extensions/SlashCommand/index.tsx@@ -7,6 +7,7 @@ import {import { ChainedCommands } from '@tiptap/core'import { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'+import contextSchema from 'editor/extensions/context/contextSchema'import Item from './Item'@@ -15,18 +16,37 @@ interface CommandItem {extendCommand: (cmd: ChainedCommands) => ChainedCommands}-const items: CommandItem[] = [+export enum InlineContextType {+ NPMPackage = 'NPM_PACKAGE'+}++export interface InlineContext {+ type: InlineContextType+ value: string+}++const inlineContextItems: InlineContext[] = [{- title: '@slack/web-api',+ type: InlineContextType.NPMPackage,+ value: '@slack/web-api',+ },+]++function createCommandItem(context: InlineContext): CommandItem {+ return {+ title: context.value,extendCommand: (cmd) => {return cmd- // return cmd.set- // .setCodeCell({- // templateID: 'nextjs-v11-components',- // language: Lang.tsx,- // })- },- },+ .setNode(contextSchema.name, {+ type: context.type,+ value: context.value,+ })+ }+ }+}++const items: CommandItem[] = [+ ...inlineContextItems.map(createCommandItem),]const SlashCommand: CommandList = forwardRef(({diff --git a/editor/extensions/context/contextSchema.ts b/editor/extensions/context/contextSchema.tsindex 619bbfcf8..2eeabedc3 100644--- a/editor/extensions/context/contextSchema.ts+++ b/editor/extensions/context/contextSchema.ts@@ -1,9 +1,7 @@import { Node } from '@tiptap/core'-import attributeHandler from 'utils/attributeHandler'import {contextCell as constants,- HTML_ATTR_PREFIX,} from 'utils/constants'export default Node.create({@@ -41,166 +39,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