-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[PULSE-36] feat: callout component for pages and issue descriptions #5856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cdbd819
feat: editor callouts
aaryan610 00cf9bf
Merge branch 'preview' of https://github.com/makeplane/plane into fea…
aaryan610 c4535ce
chore: backspace action updated
aaryan610 8f4db33
chore: update callout attributes types
aaryan610 7818369
chore: revert emoji picker changes
aaryan610 4356eeb
fix: merge conflicts resolved from preview
aaryan610 0e91448
chore: removed class atrribute
aaryan610 add87b4
chore: added sanitization for local storage values
aaryan610 64b8cfb
chore: disable emoji picker search
aaryan610 27c4e43
fix: merge conflicts resolved from preview
aaryan610 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React, { useState } from "react"; | ||
| import { NodeViewContent, NodeViewProps, NodeViewWrapper } from "@tiptap/react"; | ||
| // constants | ||
| import { COLORS_LIST } from "@/constants/common"; | ||
| // local components | ||
| import { CalloutBlockColorSelector } from "./color-selector"; | ||
| import { CalloutBlockLogoSelector } from "./logo-selector"; | ||
| // types | ||
| import { EAttributeNames, TCalloutBlockAttributes } from "./types"; | ||
| // utils | ||
| import { updateStoredBackgroundColor } from "./utils"; | ||
|
|
||
| type Props = NodeViewProps & { | ||
| node: NodeViewProps["node"] & { | ||
| attrs: TCalloutBlockAttributes; | ||
| }; | ||
| updateAttributes: (attrs: Partial<TCalloutBlockAttributes>) => void; | ||
| }; | ||
|
|
||
| export const CustomCalloutBlock: React.FC<Props> = (props) => { | ||
| const { editor, node, updateAttributes } = props; | ||
| // states | ||
| const [isEmojiPickerOpen, setIsEmojiPickerOpen] = useState(false); | ||
| const [isColorPickerOpen, setIsColorPickerOpen] = useState(false); | ||
| // derived values | ||
| const activeBackgroundColor = COLORS_LIST.find((c) => node.attrs["data-background"] === c.key)?.backgroundColor; | ||
|
|
||
| return ( | ||
| <NodeViewWrapper | ||
| className="editor-callout-component group/callout-node relative bg-custom-background-90 rounded-lg text-custom-text-100 p-4 my-2 flex items-start gap-4 transition-colors duration-500 break-words" | ||
| style={{ | ||
| backgroundColor: activeBackgroundColor, | ||
| }} | ||
| > | ||
| <CalloutBlockLogoSelector | ||
| blockAttributes={node.attrs} | ||
| disabled={!editor.isEditable} | ||
| isOpen={isEmojiPickerOpen} | ||
| handleOpen={(val) => setIsEmojiPickerOpen(val)} | ||
| updateAttributes={updateAttributes} | ||
| /> | ||
| <CalloutBlockColorSelector | ||
| disabled={!editor.isEditable} | ||
| isOpen={isColorPickerOpen} | ||
| toggleDropdown={() => setIsColorPickerOpen((prev) => !prev)} | ||
| onSelect={(val) => { | ||
| updateAttributes({ | ||
| [EAttributeNames.BACKGROUND]: val, | ||
| }); | ||
| updateStoredBackgroundColor(val); | ||
| }} | ||
| /> | ||
| <NodeViewContent as="div" className="w-full break-words" /> | ||
| </NodeViewWrapper> | ||
| ); | ||
| }; | ||
75 changes: 75 additions & 0 deletions
75
packages/editor/src/core/extensions/callout/color-selector.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { Ban, ChevronDown } from "lucide-react"; | ||
| // constants | ||
| import { COLORS_LIST } from "@/constants/common"; | ||
| // helpers | ||
| import { cn } from "@/helpers/common"; | ||
|
|
||
| type Props = { | ||
| disabled: boolean; | ||
| isOpen: boolean; | ||
| onSelect: (color: string | null) => void; | ||
| toggleDropdown: () => void; | ||
| }; | ||
|
|
||
| export const CalloutBlockColorSelector: React.FC<Props> = (props) => { | ||
| const { disabled, isOpen, onSelect, toggleDropdown } = props; | ||
|
|
||
| const handleColorSelect = (val: string | null) => { | ||
| onSelect(val); | ||
| toggleDropdown(); | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn("opacity-0 pointer-events-none absolute top-2 right-2 z-10 transition-opacity", { | ||
| "group-hover/callout-node:opacity-100 group-hover/callout-node:pointer-events-auto": !disabled, | ||
| "opacity-100 pointer-events-auto": isOpen, | ||
| })} | ||
| contentEditable={false} | ||
| > | ||
| <div className="relative"> | ||
| <button | ||
| type="button" | ||
| onClick={(e) => { | ||
| toggleDropdown(); | ||
| e.stopPropagation(); | ||
| }} | ||
| className={cn( | ||
| "flex items-center gap-1 h-full whitespace-nowrap py-1 px-2.5 text-sm font-medium text-custom-text-300 hover:bg-white/10 active:bg-custom-background-80 rounded transition-colors", | ||
| { | ||
| "bg-white/10": isOpen, | ||
| } | ||
| )} | ||
| disabled={disabled} | ||
| > | ||
| <span>Color</span> | ||
| <ChevronDown className="flex-shrink-0 size-3" /> | ||
| </button> | ||
| {isOpen && ( | ||
| <section className="absolute top-full right-0 z-10 mt-1 rounded-md border-[0.5px] border-custom-border-300 bg-custom-background-100 p-2 shadow-custom-shadow-rg animate-in fade-in slide-in-from-top-1"> | ||
| <div className="flex items-center gap-2"> | ||
| {COLORS_LIST.map((color) => ( | ||
| <button | ||
| key={color.key} | ||
| type="button" | ||
| className="flex-shrink-0 size-6 rounded border-[0.5px] border-custom-border-400 hover:opacity-60 transition-opacity" | ||
| style={{ | ||
| backgroundColor: color.backgroundColor, | ||
| }} | ||
| onClick={() => handleColorSelect(color.key)} | ||
| /> | ||
| ))} | ||
| <button | ||
| type="button" | ||
| className="flex-shrink-0 size-6 grid place-items-center rounded text-custom-text-300 border-[0.5px] border-custom-border-400 hover:bg-custom-background-80 transition-colors" | ||
| onClick={() => handleColorSelect(null)} | ||
| > | ||
| <Ban className="size-4" /> | ||
| </button> | ||
| </div> | ||
| </section> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; |
68 changes: 68 additions & 0 deletions
68
packages/editor/src/core/extensions/callout/extension-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { Node, mergeAttributes } from "@tiptap/core"; | ||
| import { Node as NodeType } from "@tiptap/pm/model"; | ||
| import { MarkdownSerializerState } from "@tiptap/pm/markdown"; | ||
| // types | ||
| import { EAttributeNames, TCalloutBlockAttributes } from "./types"; | ||
| // utils | ||
| import { DEFAULT_CALLOUT_BLOCK_ATTRIBUTES } from "./utils"; | ||
|
|
||
| // Extend Tiptap's Commands interface | ||
| declare module "@tiptap/core" { | ||
| interface Commands<ReturnType> { | ||
| calloutComponent: { | ||
| insertCallout: () => ReturnType; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export const CustomCalloutExtensionConfig = Node.create({ | ||
| name: "calloutComponent", | ||
| group: "block", | ||
| content: "block+", | ||
|
|
||
| addAttributes() { | ||
| const attributes = { | ||
| // Reduce instead of map to accumulate the attributes directly into an object | ||
| ...Object.values(EAttributeNames).reduce((acc, value) => { | ||
| acc[value] = { | ||
| default: DEFAULT_CALLOUT_BLOCK_ATTRIBUTES[value], | ||
| }; | ||
| return acc; | ||
| }, {}), | ||
| }; | ||
| return attributes; | ||
| }, | ||
|
|
||
| addStorage() { | ||
| return { | ||
| markdown: { | ||
| serialize(state: MarkdownSerializerState, node: NodeType) { | ||
| const attrs = node.attrs as TCalloutBlockAttributes; | ||
| const logoInUse = attrs["data-logo-in-use"]; | ||
| // add callout logo | ||
| if (logoInUse === "emoji") { | ||
| state.write( | ||
| `> <img src="${attrs["data-emoji-url"]}" alt="${attrs["data-emoji-unicode"]}" width="30px" />\n` | ||
| ); | ||
| } else { | ||
| state.write(`> <icon>${attrs["data-icon-name"]} icon</icon>\n`); | ||
| } | ||
aaryan610 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // add an empty line after the logo | ||
| state.write("> \n"); | ||
| // add '> ' before each line of the callout content | ||
| state.wrapBlock("> ", null, node, () => state.renderContent(node)); | ||
| state.closeBlock(node); | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| parseHTML() { | ||
| return [{ tag: "callout-component" }]; | ||
| }, | ||
|
|
||
| // Render HTML for the callout node | ||
| renderHTML({ HTMLAttributes }) { | ||
| return ["callout-component", mergeAttributes(HTMLAttributes)]; | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { findParentNodeClosestToPos, Predicate, ReactNodeViewRenderer } from "@tiptap/react"; | ||
| // extensions | ||
| import { CustomCalloutBlock } from "@/extensions"; | ||
| // helpers | ||
| import { insertEmptyParagraphAtNodeBoundaries } from "@/helpers/insert-empty-paragraph-at-node-boundary"; | ||
| // config | ||
| import { CustomCalloutExtensionConfig } from "./extension-config"; | ||
| // utils | ||
| import { getStoredBackgroundColor, getStoredLogo } from "./utils"; | ||
|
|
||
| export const CustomCalloutExtension = CustomCalloutExtensionConfig.extend({ | ||
| selectable: true, | ||
| draggable: true, | ||
|
|
||
| addCommands() { | ||
| return { | ||
| insertCallout: | ||
| () => | ||
| ({ commands }) => { | ||
Palanikannan1437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // get stored logo values and background color from the local storage | ||
| const storedLogoValues = getStoredLogo(); | ||
| const storedBackgroundValue = getStoredBackgroundColor(); | ||
|
|
||
Palanikannan1437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return commands.insertContent({ | ||
| type: this.name, | ||
| content: [ | ||
| { | ||
| type: "paragraph", | ||
| }, | ||
| ], | ||
| attrs: { | ||
| ...storedLogoValues, | ||
| "data-background": storedBackgroundValue, | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| addKeyboardShortcuts() { | ||
| return { | ||
| Backspace: ({ editor }) => { | ||
| const { $from, empty } = editor.state.selection; | ||
| try { | ||
| const isParentNodeCallout: Predicate = (node) => node.type === this.type; | ||
| const parentNodeDetails = findParentNodeClosestToPos($from, isParentNodeCallout); | ||
| // Check if selection is empty and at the beginning of the callout | ||
| if (empty && parentNodeDetails) { | ||
| const isCursorAtCalloutBeginning = $from.pos === parentNodeDetails.start + 1; | ||
| if (parentNodeDetails.node.content.size > 2 && isCursorAtCalloutBeginning) { | ||
| editor.commands.setTextSelection(parentNodeDetails.pos - 1); | ||
| return true; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error("Error in performing backspace action on callout", error); | ||
| } | ||
| return false; // Allow the default behavior if conditions are not met | ||
| }, | ||
| ArrowDown: insertEmptyParagraphAtNodeBoundaries("down", this.name), | ||
| ArrowUp: insertEmptyParagraphAtNodeBoundaries("up", this.name), | ||
| }; | ||
| }, | ||
|
|
||
| addNodeView() { | ||
| return ReactNodeViewRenderer(CustomCalloutBlock); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from "./block"; | ||
| export * from "./extension"; | ||
| export * from "./read-only-extension"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.