π₯π₯π₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.
TypeScript boardsbug-trackerdjangodockerganttissue-trackerjirajira-alternativekanbanlinearpostgresqlproduct-managementproject-managementproject-planningpythonreactredistypesciptvitework-management
[WIKI-542] refactor: editor extensions (#7345)
Aaryan Khandelwalc067eaa1ed3c
diff --git a/packages/editor/src/core/extensions/code/index.tsx b/packages/editor/src/core/extensions/code/index.tsxindex 15d66a6bae..7c679e92e2 100644--- a/packages/editor/src/core/extensions/code/index.tsx+++ b/packages/editor/src/core/extensions/code/index.tsx@@ -118,4 +118,7 @@ export const CustomCodeBlockExtension = CodeBlockLowlight.extend({lowlight,defaultLanguage: "plaintext",exitOnTripleEnter: false,+ HTMLAttributes: {+ class: "",+ },});diff --git a/packages/editor/src/core/extensions/custom-link/extension.tsx b/packages/editor/src/core/extensions/custom-link/extension.tsxindex 182afc9f8f..9be125ce6b 100644--- a/packages/editor/src/core/extensions/custom-link/extension.tsx+++ b/packages/editor/src/core/extensions/custom-link/extension.tsx@@ -3,6 +3,8 @@ import { Plugin } from "@tiptap/pm/state";import { find, registerCustomProtocol, reset } from "linkifyjs";// constantsimport { CORE_EXTENSIONS } from "@/constants/extension";+// helpers+import { isValidHttpUrl } from "@/helpers/common";// local importsimport { autolink } from "./helpers/autolink";import { clickHandler } from "./helpers/clickHandler";@@ -112,13 +114,14 @@ export const CustomLinkExtension = Mark.create<LinkOptions, CustomLinkStorage>({linkOnPaste: true,autolink: true,inclusive: false,- protocols: [],+ protocols: ["http", "https"],HTMLAttributes: {target: "_blank",rel: "noopener noreferrer nofollow",- class: null,+ class:+ "text-custom-primary-300 underline underline-offset-[3px] hover:text-custom-primary-500 transition-colors cursor-pointer",},- validate: undefined,+ validate: (url: string) => isValidHttpUrl(url).isValid,};},diff --git a/packages/editor/src/core/extensions/extensions.ts b/packages/editor/src/core/extensions/extensions.tsindex 1833dec461..27103693b2 100644--- a/packages/editor/src/core/extensions/extensions.ts+++ b/packages/editor/src/core/extensions/extensions.ts@@ -1,14 +1,10 @@import { Extensions } from "@tiptap/core";import CharacterCount from "@tiptap/extension-character-count";-import Placeholder from "@tiptap/extension-placeholder";import TaskItem from "@tiptap/extension-task-item";import TaskList from "@tiptap/extension-task-list";import TextStyle from "@tiptap/extension-text-style";import TiptapUnderline from "@tiptap/extension-underline";-import StarterKit from "@tiptap/starter-kit";import { Markdown } from "tiptap-markdown";-// constants-import { CORE_EXTENSIONS } from "@/constants/extension";// extensionsimport {CustomCalloutExtension,@@ -30,9 +26,6 @@ import {TableRow,UtilityExtension,} from "@/extensions";-// helpers-import { isValidHttpUrl } from "@/helpers/common";-import { getExtensionStorage } from "@/helpers/get-extension-storage";// plane editor extensionsimport { CoreEditorAdditionalExtensions } from "@/plane-editor/extensions";// types@@ -40,6 +33,8 @@ import type { IEditorProps } from "@/types";// local importsimport { CustomImageExtension } from "./custom-image/extension";import { EmojiExtension } from "./emoji/extension";+import { CustomPlaceholderExtension } from "./placeholder";+import { CustomStarterKitExtension } from "./starter-kit";type TArguments = Pick<IEditorProps,@@ -62,62 +57,15 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {} = args;const extensions = [- StarterKit.configure({- bulletList: {- HTMLAttributes: {- class: "list-disc pl-7 space-y-[--list-spacing-y]",- },- },- orderedList: {- HTMLAttributes: {- class: "list-decimal pl-7 space-y-[--list-spacing-y]",- },- },- listItem: {- HTMLAttributes: {- class: "not-prose space-y-2",- },- },- code: false,- codeBlock: false,- horizontalRule: false,- blockquote: false,- paragraph: {- HTMLAttributes: {- class: "editor-paragraph-block",- },- },- heading: {- HTMLAttributes: {- class: "editor-heading-block",- },- },- dropcursor: {- class:- "text-custom-text-300 transition-all motion-reduce:transition-none motion-reduce:hover:transform-none duration-200 ease-[cubic-bezier(0.165, 0.84, 0.44, 1)]",- },- ...(enableHistory ? {} : { history: false }),+ CustomStarterKitExtension({+ enableHistory,}),EmojiExtension,CustomQuoteExtension,- CustomHorizontalRule.configure({- HTMLAttributes: {- class: "py-4 border-custom-border-400",- },- }),+ CustomHorizontalRule,CustomKeymap,ListKeymap({ tabIndex }),- CustomLinkExtension.configure({- openOnClick: true,- autolink: true,- linkOnPaste: true,- protocols: ["http", "https"],- validate: (url: string) => isValidHttpUrl(url).isValid,- HTMLAttributes: {- class:- "text-custom-primary-300 underline underline-offset-[3px] hover:text-custom-primary-500 transition-colors cursor-pointer",- },- }),+ CustomLinkExtension,CustomTypographyExtension,TiptapUnderline,TextStyle,@@ -132,11 +80,7 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {},nested: true,}),- CustomCodeBlockExtension.configure({- HTMLAttributes: {- class: "",- },- }),+ CustomCodeBlockExtension,CustomCodeInlineExtension,Markdown.configure({html: true,@@ -149,34 +93,9 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {TableCell,TableRow,CustomMentionExtension(mentionHandler),- Placeholder.configure({- placeholder: ({ editor, node }) => {- if (!editor.isEditable) return "";-- if (node.type.name === CORE_EXTENSIONS.HEADING) return `Heading ${node.attrs.level}`;-- const isUploadInProgress = getExtensionStorage(editor, CORE_EXTENSIONS.UTILITY)?.uploadInProgress;-- if (isUploadInProgress) return "";-- const shouldHidePlaceholder =- editor.isActive(CORE_EXTENSIONS.TABLE) ||- editor.isActive(CORE_EXTENSIONS.CODE_BLOCK) ||- editor.isActive(CORE_EXTENSIONS.IMAGE) ||- editor.isActive(CORE_EXTENSIONS.CUSTOM_IMAGE);-- if (shouldHidePlaceholder) return "";-- if (placeholder) {- if (typeof placeholder === "string") return placeholder;- else return placeholder(editor.isFocused, editor.getHTML());- }-- return "Press '/' for commands...";- },- includeChildren: true,- }),+ CustomPlaceholderExtension({ placeholder }),CharacterCount,+ CustomColorExtension,CustomTextAlignExtension,CustomCalloutExtension,UtilityExtension({@@ -184,7 +103,6 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {fileHandler,isEditable: editable,}),- CustomColorExtension,...CoreEditorAdditionalExtensions({disabledExtensions,flaggedExtensions,diff --git a/packages/editor/src/core/extensions/horizontal-rule.ts b/packages/editor/src/core/extensions/horizontal-rule.tsindex 99a5dacc3e..4ffbcbb00f 100644--- a/packages/editor/src/core/extensions/horizontal-rule.ts+++ b/packages/editor/src/core/extensions/horizontal-rule.ts@@ -20,15 +20,16 @@ declare module "@tiptap/core" {export const CustomHorizontalRule = Node.create<HorizontalRuleOptions>({name: CORE_EXTENSIONS.HORIZONTAL_RULE,+ group: "block",addOptions() {return {- HTMLAttributes: {},+ HTMLAttributes: {+ class: "py-4 border-custom-border-400",+ },};},- group: "block",-parseHTML() {return [{diff --git a/packages/editor/src/core/extensions/placeholder.ts b/packages/editor/src/core/extensions/placeholder.tsnew file mode 100644index 0000000000..9e23792a2f--- /dev/null+++ b/packages/editor/src/core/extensions/placeholder.ts@@ -0,0 +1,43 @@+import Placeholder from "@tiptap/extension-placeholder";+// constants+import { CORE_EXTENSIONS } from "@/constants/extension";+// helpers+import { getExtensionStorage } from "@/helpers/get-extension-storage";+// types+import type { IEditorProps } from "@/types";++type TArgs = {+ placeholder: IEditorProps["placeholder"];+};++export const CustomPlaceholderExtension = (args: TArgs) => {+ const { placeholder } = args;++ return Placeholder.configure({+ placeholder: ({ editor, node }) => {+ if (!editor.isEditable) return "";++ if (node.type.name === CORE_EXTENSIONS.HEADING) return `Heading ${node.attrs.level}`;++ const isUploadInProgress = getExtensionStorage(editor, CORE_EXTENSIONS.UTILITY)?.uploadInProgress;++ if (isUploadInProgress) return "";++ const shouldHidePlaceholder =+ editor.isActive(CORE_EXTENSIONS.TABLE) ||+ editor.isActive(CORE_EXTENSIONS.CODE_BLOCK) ||+ editor.isActive(CORE_EXTENSIONS.IMAGE) ||+ editor.isActive(CORE_EXTENSIONS.CUSTOM_IMAGE);++ if (shouldHidePlaceholder) return "";++ if (placeholder) {+ if (typeof placeholder === "string") return placeholder;+ else return placeholder(editor.isFocused, editor.getHTML());+ }++ return "Press '/' for commands...";+ },+ includeChildren: true,+ });+};diff --git a/packages/editor/src/core/extensions/read-only-extensions.ts b/packages/editor/src/core/extensions/read-only-extensions.tsindex 25541dd3cc..4d627b7f92 100644--- a/packages/editor/src/core/extensions/read-only-extensions.ts+++ b/packages/editor/src/core/extensions/read-only-extensions.ts@@ -4,7 +4,6 @@ import TaskItem from "@tiptap/extension-task-item";import TaskList from "@tiptap/extension-task-list";import TextStyle from "@tiptap/extension-text-style";import TiptapUnderline from "@tiptap/extension-underline";-import StarterKit from "@tiptap/starter-kit";import { Markdown } from "tiptap-markdown";// extensionsimport {@@ -25,8 +24,6 @@ import {UtilityExtension,ImageExtension,} from "@/extensions";-// helpers-import { isValidHttpUrl } from "@/helpers/common";// plane editor extensionsimport { CoreReadOnlyEditorAdditionalExtensions } from "@/plane-editor/extensions";// types@@ -34,6 +31,7 @@ import type { IReadOnlyEditorProps } from "@/types";// local importsimport { CustomImageExtension } from "./custom-image/extension";import { EmojiExtension } from "./emoji/extension";+import { CustomStarterKitExtension } from "./starter-kit";type Props = Pick<IReadOnlyEditorProps, "disabledExtensions" | "flaggedExtensions" | "fileHandler" | "mentionHandler">;@@ -41,57 +39,13 @@ export const CoreReadOnlyEditorExtensions = (props: Props): Extensions => {const { disabledExtensions, fileHandler, flaggedExtensions, mentionHandler } = props;const extensions = [- StarterKit.configure({- bulletList: {- HTMLAttributes: {- class: "list-disc pl-7 space-y-[--list-spacing-y]",- },- },- orderedList: {- HTMLAttributes: {- class: "list-decimal pl-7 space-y-[--list-spacing-y]",- },- },- listItem: {- HTMLAttributes: {- class: "not-prose space-y-2",- },- },- code: false,- codeBlock: false,- horizontalRule: false,- blockquote: false,- paragraph: {- HTMLAttributes: {- class: "editor-paragraph-block",- },- },- heading: {- HTMLAttributes: {- class: "editor-heading-block",- },- },- dropcursor: false,- gapcursor: false,+ CustomStarterKitExtension({+ enableHistory: false,}),EmojiExtension,CustomQuoteExtension,- CustomHorizontalRule.configure({- HTMLAttributes: {- class: "py-4 border-custom-border-400",- },- }),- CustomLinkExtension.configure({- openOnClick: true,- autolink: true,- linkOnPaste: true,- protocols: ["http", "https"],- validate: (url: string) => isValidHttpUrl(url).isValid,- HTMLAttributes: {- class:- "text-custom-primary-300 underline underline-offset-[3px] hover:text-custom-primary-500 transition-colors cursor-pointer",- },- }),+ CustomHorizontalRule,+ CustomLinkExtension,CustomTypographyExtension,TiptapUnderline,TextStyle,@@ -106,11 +60,7 @@ export const CoreReadOnlyEditorExtensions = (props: Props): Extensions => {},nested: true,}),- CustomCodeBlockExtension.configure({- HTMLAttributes: {- class: "",- },- }),+ CustomCodeBlockExtension,CustomCodeInlineExtension,Markdown.configure({html: true,diff --git a/packages/editor/src/core/extensions/starter-kit.ts b/packages/editor/src/core/extensions/starter-kit.tsnew file mode 100644index 0000000000..e6a4c968d7--- /dev/null+++ b/packages/editor/src/core/extensions/starter-kit.ts@@ -0,0 +1,46 @@+import StarterKit from "@tiptap/starter-kit";++type TArgs = {+ enableHistory: boolean;+};++export const CustomStarterKitExtension = (args: TArgs) => {+ const { enableHistory } = args;++ return StarterKit.configure({+ bulletList: {+ HTMLAttributes: {+ class: "list-disc pl-7 space-y-[--list-spacing-y]",+ },+ },+ orderedList: {+ HTMLAttributes: {+ class: "list-decimal pl-7 space-y-[--list-spacing-y]",+ },+ },+ listItem: {+ HTMLAttributes: {+ class: "not-prose space-y-2",+ },+ },+ code: false,+ codeBlock: false,+ horizontalRule: false,+ blockquote: false,+ paragraph: {+ HTMLAttributes: {+ class: "editor-paragraph-block",+ },+ },+ heading: {+ HTMLAttributes: {+ class: "editor-heading-block",+ },+ },+ dropcursor: {+ class:+ "text-custom-text-300 transition-all motion-reduce:transition-none motion-reduce:hover:transform-none duration-200 ease-[cubic-bezier(0.165, 0.84, 0.44, 1)]",+ },+ ...(enableHistory ? {} : { history: false }),+ });+};