Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Impove editor styling and add slash command + context node [WIP]
Tomas Valenta4513fbc15213
diff --git a/components/DeleteButton.tsx b/components/DeleteButton.tsxindex aaa022140..4b84fc764 100644--- a/components/DeleteButton.tsx+++ b/components/DeleteButton.tsx@@ -1,4 +1,5 @@import clsx from 'clsx'+import { Trash2, X } from 'lucide-react'import { useState, useEffect, MouseEvent } from 'react'import Text from './Text'diff --git a/components/Editor/RouteEditor/PromptEditor.tsx b/components/Editor/RouteEditor/PromptEditor.tsxindex a16992bf0..b49573284 100644--- a/components/Editor/RouteEditor/PromptEditor.tsx+++ b/components/Editor/RouteEditor/PromptEditor.tsx@@ -17,10 +17,10 @@ function PromptEditor({ onChange, block }: Props) {return (<div className="flex- bg-white- p-2min-w-fullrounded+ border+ bg-slate-300"><EditorContenteditor={editor}diff --git a/components/Editor/RouteEditor/RequestBodyEditor.tsx b/components/Editor/RouteEditor/RequestBodyEditor.tsxindex 5f5bb35e9..02cc4528a 100644--- a/components/Editor/RouteEditor/RequestBodyEditor.tsx+++ b/components/Editor/RouteEditor/RequestBodyEditor.tsx@@ -55,8 +55,6 @@ function RequestBodyEditor({'text-slate-500','focus:text-slate-600','no-scroller',- 'transition-[shadow,colors]',- 'focus:shadow','bg-white','outline-none','text-sm',diff --git a/components/Editor/RouteEditor/index.tsx b/components/Editor/RouteEditor/index.tsxindex d08f12796..757a1a06a 100644--- a/components/Editor/RouteEditor/index.tsx+++ b/components/Editor/RouteEditor/index.tsx@@ -23,6 +23,7 @@ function RouteEditor({ route }: Props) {px-4flexflex-1+ bg-whiteitems-startjustify-centeroverflow-hidden@@ -37,19 +38,12 @@ function RouteEditor({ route }: Props) {}{route &&<>- <div className="flex items-center space-x-2">- <Select- direction="left"- selectedValue={{ key: route?.method, title: route.method.toUpperCase() }}- values={methods.map(m => ({ key: m, title: m.toUpperCase() }))}- onChange={m => changeRoute(route.id, { method: m.key as Method })}- />- </div><div className="flexflex-1flex-col- items-start+ items-center+ max-w-[65ch]overflow-autoscrollerspace-y-2diff --git a/components/Editor/Routes.tsx b/components/Editor/Routes.tsxindex 455aa4cea..eaa41d0e6 100644--- a/components/Editor/Routes.tsx+++ b/components/Editor/Routes.tsx@@ -1,26 +1,31 @@import clsx from 'clsx'import { Plus } from 'lucide-react'-import { Route } from 'state/store'+import { Method, methods, Route } from 'state/store'import Button from 'components/Button'import Text from 'components/Text'import DeleteButton from 'components/DeleteButton'+import Select from 'components/Select'+import { useStateStore } from 'state/StoreProvider'export interface Props {routes: Route[]selectRoute: (id: string) => voidselectedRouteID?: string- addRoute: () => voiddeleteRoute: (id: string) => void}function Routes({routes,selectedRouteID,- addRoute,deleteRoute,selectRoute,}: Props) {+ const store = useStateStore()++ const addRoute = store.use.addRoute()+ const changeRoute = store.use.changeRoute()+return (<div><div@@ -50,14 +55,12 @@ function Routes({/><div className="flex- flex-coloverflow-y-autoleading-4py-1.5px-2break-wordswhitespace-normal- space-y-0.5">{routes.map(r =><div@@ -67,21 +70,35 @@ function Routes({items-centerjustify-betweenhover:text-green-800- roundedpx-2`,{- 'text-green-800': selectedRouteID === r.id,+ 'text-green-800 group': selectedRouteID === r.id,+ 'text-slate-300': selectedRouteID !== r.id,})}><buttononClick={() => selectRoute(r.id)}+ className="+ flex+ space-x-1+ "><TextclassName="font-mono"- text={r.method.toUpperCase() + ' ' + r.route}+ text={r.route}/>+ <div className="flex items-center space-x-2">+ <Select+ direction="left"+ selectedValue={{ key: r.method, title: r.method.toUpperCase() }}+ values={methods.map(m => ({ key: m, title: m.toUpperCase() }))}+ onChange={m => changeRoute(r.id, { method: m.key as Method })}+ isSelected={r.id === selectedRouteID}+ />+ </div>+</button><DeleteButtononDelete={() => deleteRoute(r.id)}diff --git a/components/Editor/extensions/SlackCommand/Item.tsx b/components/Editor/extensions/SlashCommand/Item.tsxsimilarity index 100%rename from components/Editor/extensions/SlackCommand/Item.tsxrename to components/Editor/extensions/SlashCommand/Item.tsxdiff --git a/components/Editor/extensions/SlackCommand/index.tsx b/components/Editor/extensions/SlashCommand/index.tsxsimilarity index 64%rename from components/Editor/extensions/SlackCommand/index.tsxrename to components/Editor/extensions/SlashCommand/index.tsxindex 3a1aebf40..7c109efac 100644--- a/components/Editor/extensions/SlackCommand/index.tsx+++ b/components/Editor/extensions/SlashCommand/index.tsx@@ -7,7 +7,6 @@ import {import { ChainedCommands } from '@tiptap/core'import { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'-import { Lang } from 'editor/extensions/codeCell/language'import Item from './Item'@@ -21,54 +20,11 @@ const items: CommandItem[] = [title: 'Next.js Code Cell',extendCommand: (cmd) => {return cmd- .setCodeCell({- templateID: 'nextjs-v11-components',- language: Lang.tsx,- })- },- },- {- title: 'Node.js Code Cell',- extendCommand: (cmd) => {- return cmd- .setCodeCell({- templateID: 'nodejs-v16',- language: Lang.ts,- })- },- },- {- title: 'Shell Command (Next.js)',- extendCommand: (last) => {- return last.setCodeCell({- templateID: 'nextjs-v11-components',- language: Lang.sh,- })- },- },- {- title: 'Shell Command (Node.js)',- extendCommand: (last) => {- return last.setCodeCell({- templateID: 'nodejs-v16',- language: Lang.sh,- })- }- },- {- title: 'Iframe Cell',- extendCommand: (cmd) => {- return cmd- .focus()- .setIframe({ src: 'https://usedevbook.com' })- },- },- {- title: 'Console Cell',- extendCommand: (cmd) => {- return cmd- .focus()- .setConsoleCell()+ // return cmd.set+ // .setCodeCell({+ // templateID: 'nextjs-v11-components',+ // language: Lang.tsx,+ // })},},]@@ -154,4 +110,4 @@ const SlashCommand: CommandList = forwardRef(({SlashCommand.displayName = 'SlashCommand'-export default SlashCommand\ No newline at end of file+export default SlashCommanddiff --git a/components/Editor/index.tsx b/components/Editor/index.tsxindex c22e8541a..f1be779c8 100644--- a/components/Editor/index.tsx+++ b/components/Editor/index.tsx@@ -18,7 +18,6 @@ function Editor({ project }: Props) {// TODO: Handle editor state differently so we don't rerender this component on each editor edit.const routes = store.use.routes()- const addRoute = store.use.addRoute()const deleteRoute = store.use.deleteRoute()const [selectedRouteID, setSelectedRouteID] = useState(() => routes.length > 0 ? routes[0].id : undefined)@@ -69,7 +68,6 @@ function Editor({ project }: Props) {selectRoute={setSelectedRouteID}selectedRouteID={selectedRoute?.id}deleteRoute={handleDeleteRoute}- addRoute={addRoute}/><RouteEditorroute={selectedRoute}diff --git a/components/Select/index.tsx b/components/Select/index.tsxindex be914b8fe..7778da521 100644--- a/components/Select/index.tsx+++ b/components/Select/index.tsx@@ -17,10 +17,12 @@ export interface Props {onChange: (v: Value) => void// Where to align the modaldirection: 'left' | 'right'+ isSelected?: boolean}function Select({values,+ isSelected,selectedValue,onChange,direction,@@ -56,16 +58,15 @@ function Select({`transition-allcursor-pointerflex- hover:text-green-600+ hover:text-green-800items-centerjustify-betweenspace-x-1`,{- 'text-green-600': isOpened,- 'text-green-500': !isOpened,+ 'text-green-800': isSelected,+ 'text-slate-300': !isSelected,})}- onClick={() => setIsOpened(val => !val)}><TextclassName="font-mono"@@ -74,9 +75,9 @@ function Select({<ChevronDownIconclassName="self-center"size={14}+ onClick={() => setIsOpened(val => !val)}/></div>-{/* Modal */}{isOpened && (<div className={clsx(diff --git a/editor/extensions/context/contextSchema.ts b/editor/extensions/context/contextSchema.tsnew file mode 100644index 000000000..2503b6c13--- /dev/null+++ b/editor/extensions/context/contextSchema.ts@@ -0,0 +1,205 @@+import { Node } from '@tiptap/core'++import attributeHandler from 'utils/attributeHandler'+import {+ contextCell as constants,+ HTML_ATTR_PREFIX,+} from 'utils/constants'++export default Node.create({+ name: constants.CONTEXT_NODE_NAME,+ content: 'text*',+ marks: '',+ atom: false,+ selectable: true,+ group: 'inline',+ isolating: true,+ defining: true,++ addAttributes() {+ return {+ }+ },++ parseHTML() {+ return [+ {+ tag: 'pre',+ preserveWhitespace: 'full',+ },+ ]+ },++ renderHTML({ HTMLAttributes }) {+ return [+ 'pre',+ HTMLAttributes,+ ['code', 0],+ ]+ },+})+++// 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 filediff --git a/editor/extensions/slashCommand.ts b/editor/extensions/slashCommand.tsindex 4678f45e1..9bd3ea0bc 100644--- a/editor/extensions/slashCommand.ts+++ b/editor/extensions/slashCommand.ts@@ -1,4 +1,4 @@-import SlashCommand from 'src/components/Editor/extensions/SlashCommand'+import SlashCommand from 'components/Editor/extensions/SlashCommand'import { Command } from './command'@@ -11,6 +11,6 @@ export default Command(SlashCommand).configure({suggestion: {char: '/',- startOfLine: true,+ startOfLine: false,},})diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.tsindex 4ed6796a6..f89934086 100644--- a/hooks/useDocEditor.ts+++ b/hooks/useDocEditor.ts@@ -27,7 +27,7 @@ const extensions = [orderedList: false,}),Placeholder.configure({- placeholder: "Type '/' for commands",+ placeholder: "All You Need Is English",}),SlashCommand,]diff --git a/package.json b/package.jsonindex e86c75440..a8996991a 100644--- a/package.json+++ b/package.json@@ -48,6 +48,7 @@"zustand": "^4.3.5"},"devDependencies": {+ "@tailwindcss/typography": "^0.5.9","@types/node": "^18.14.6","@types/randomcolor": "^0.5.7","@types/react": "^18.0.28",diff --git a/styles/global.css b/styles/global.cssindex 35edcc829..b756a2be7 100644--- a/styles/global.css+++ b/styles/global.css@@ -1,4 +1,5 @@@import 'highlight.js/styles/a11y-dark.css';+@import './prosemirror.css';@tailwind base;@tailwind components;diff --git a/styles/prosemirror.css b/styles/prosemirror.cssnew file mode 100644index 000000000..bbb5670bb--- /dev/null+++ b/styles/prosemirror.css@@ -0,0 +1,73 @@+.ProseMirror {+ @apply whitespace-pre-wrap prose prose-sm;+ @apply max-w-[50rem];+ @apply whitespace-pre-wrap;++ /*+ * NOTE: Disabling overflow anchor fixes a code cell scrolling bug+ * that happens in Chrome:+ * If you have a code cell with `BottomResizer`, resize the code cell+ * so that it has a scrollbar, scroll at the bottom and start deleting+ * new lines, the outer scrollbar of ProseMirror starts moving with+ * every deleted new-line which then moves the whole document page.+ *+ * I'm currently not sure how to fix the bug in any other way. Maybe+ * some enclosing elemens have their height incorrectly calculated?+ *+ * More info and solution might be found here:+ * - https://discuss.prosemirror.net/t/undesired-scrolling-on-updatestate/2066+ * - https://github.com/ProseMirror/prosemirror/issues/933+ */+ overflow-anchor: none;+}++.ProseMirror:focus {+ @apply outline-none;+}++.ProseMirror p,+.ProseMirror li,+.ProseMirror ul,+.ProseMirror h1,+.ProseMirror h2,+.ProseMirror h3 {+ @apply max-w-full;+}++.ProseMirror h1,+.ProseMirror h2 {+ @apply py-2;+ @apply border-b border-gray-400;+}++.ProseMirror p {+ @apply mx-0 my-1;+ @apply whitespace-pre-wrap break-words;+}++/* Placeholder (on every new line) */+.ProseMirror p.is-empty::before {+ @apply float-left h-0 select-none text-slate-300;+ content: attr(data-placeholder);+}++.ProseMirror li {+ @apply relative;+}++.ProseMirror ul,+.ProseMirror ol {+ @apply pl-8;+}++.ProseMirror code {+ @apply px-1.5 py-1;+ @apply font-normal;+ @apply rounded bg-gray-300;+}++@keyframes ProseMirror-cursor-blink {+ to {+ visibility: hidden;+ }+}diff --git a/tailwind.config.js b/tailwind.config.jsindex 0a6dab713..974a081af 100644--- a/tailwind.config.js+++ b/tailwind.config.js@@ -22,4 +22,7 @@ module.exports = {},},},+ plugins: [+ require('@tailwindcss/typography'),+ ],}diff --git a/utils/attributeHandler.ts b/utils/attributeHandler.tsnew file mode 100644index 000000000..ba7b523f6--- /dev/null+++ b/utils/attributeHandler.ts@@ -0,0 +1,23 @@+function attributeHandler({+ htmlPrefix,+ nodeAttribute,+}: {+ htmlPrefix?: string,+ nodeAttribute: string,+}) {+ const htmlAttribute = `${htmlPrefix ? htmlPrefix : ''}${nodeAttribute}`++ return {+ [nodeAttribute]: {+ default: undefined,+ parseHTML: (element: HTMLElement) => ({+ [nodeAttribute]: element.getAttribute(htmlAttribute),+ }),+ renderHTML: (attributes: Record<string, any>) => ({+ ...attributes[nodeAttribute] && { [htmlAttribute]: attributes[nodeAttribute] },+ }),+ },+ }+}++export default attributeHandlerdiff --git a/utils/constants.ts b/utils/constants.tsnew file mode 100644index 000000000..c882d9548--- /dev/null+++ b/utils/constants.ts@@ -0,0 +1,7 @@+export const HTML_ATTR_PREFIX = 'data-'++const CONTEXT_NODE_NAME = 'context'++export const contextCell = {+ CONTEXT_NODE_NAME,+}diff --git a/yarn.lock b/yarn.lockindex 6d90cdd2e..862918d20 100644--- a/yarn.lock+++ b/yarn.lock@@ -304,6 +304,16 @@dependencies:tslib "^2.4.0"+"@tailwindcss/typography@^0.5.9":+ version "0.5.9"+ resolved "https://registry.yarnpkg.com/@tailwindcss/typography/-/typography-0.5.9.tgz#027e4b0674929daaf7c921c900beee80dbad93e8"+ integrity sha512-t8Sg3DyynFysV9f4JDOVISGsjazNb48AeIYQwcL+Bsq5uf4RYL75C1giZ43KISjeDGBaTN3Kxh7Xj/vRSMJUUg==+ dependencies:+ lodash.castarray "^4.4.0"+ lodash.isplainobject "^4.0.6"+ lodash.merge "^4.6.2"+ postcss-selector-parser "6.0.10"+"@tiptap/core@^2.0.0-beta.220":version "2.0.0-beta.220"resolved "https://registry.yarnpkg.com/@tiptap/core/-/core-2.0.0-beta.220.tgz#ced4b8f13ad6361f957275510bd0c005de29d18c"@@ -2373,6 +2383,16 @@ locate-path@^6.0.0:dependencies:p-locate "^5.0.0"+lodash.castarray@^4.4.0:+ version "4.4.0"+ resolved "https://registry.yarnpkg.com/lodash.castarray/-/lodash.castarray-4.4.0.tgz#c02513515e309daddd4c24c60cfddcf5976d9115"+ integrity sha512-aVx8ztPv7/2ULbArGJ2Y42bG1mEQ5mGjpdvrbJcJFU3TbYybe+QlLS4pst9zV52ymy2in1KpFPiZnAOATxD4+Q==++lodash.isplainobject@^4.0.6:+ version "4.0.6"+ resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"+ integrity sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==+lodash.memoize@^4.1.2:version "4.1.2"resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"@@ -3264,6 +3284,14 @@ postcss-reduce-transforms@^5.1.0:dependencies:postcss-value-parser "^4.2.0"+ version "6.0.10"+ resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.10.tgz#79b61e2c0d1bfc2602d549e11d0876256f8df88d"+ integrity sha512-IQ7TZdoaqbT+LCpShg46jnZVlhWD2w6iQYAcYXfHARZ7X1t/UGhhceQDs5X0cGqKvYlHNOuv7Oa1xmb0oQuA3w==+ dependencies:+ cssesc "^3.0.0"+ util-deprecate "^1.0.2"+postcss-selector-parser@^6.0.10, postcss-selector-parser@^6.0.11, postcss-selector-parser@^6.0.4, postcss-selector-parser@^6.0.5, postcss-selector-parser@^6.0.9:version "6.0.11"resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.11.tgz#2e41dc39b7ad74046e1615185185cd0b17d0c8dc"