-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4097 from udecode/templates/45
- Loading branch information
Showing
58 changed files
with
2,747 additions
and
848 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
35 changes: 35 additions & 0 deletions
35
...lates/plate-playground-template/src/components/editor/plugins/block-selection-plugins.tsx
This file contains 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,35 @@ | ||
'use client'; | ||
|
||
import { BlockSelectionPlugin } from '@udecode/plate-selection/react'; | ||
|
||
import { BlockSelection } from '@/components/plate-ui/block-selection'; | ||
|
||
export const blockSelectionPlugins = [ | ||
BlockSelectionPlugin.configure(({ editor }) => ({ | ||
options: { | ||
enableContextMenu: true, | ||
isSelectable: (element, path) => { | ||
return ( | ||
!['code_line', 'column', 'td'].includes(element.type) && | ||
!editor.api.block({ above: true, at: path, match: { type: 'tr' } }) | ||
); | ||
}, | ||
}, | ||
render: { | ||
belowRootNodes: (props) => { | ||
if (!props.className?.includes('slate-selectable')) return null; | ||
|
||
return <BlockSelection />; | ||
}, | ||
}, | ||
})), | ||
] as const; | ||
|
||
export const blockSelectionReadOnlyPlugin = BlockSelectionPlugin.configure({ | ||
api: {}, | ||
extendEditor: null, | ||
handlers: {}, | ||
options: {}, | ||
render: {}, | ||
useHooks: null, | ||
}); |
143 changes: 82 additions & 61 deletions
143
templates/plate-playground-template/src/components/editor/plugins/comments-plugin.tsx
This file contains 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 |
---|---|---|
@@ -1,65 +1,86 @@ | ||
'use client'; | ||
|
||
import type { TComment } from '@udecode/plate-comments'; | ||
|
||
import { CommentsPlugin } from '@udecode/plate-comments/react'; | ||
|
||
import { CommentsPopover } from '@/components/plate-ui/comments-popover'; | ||
|
||
const commentsData: Record<string, TComment> = { | ||
1: { | ||
id: '1', | ||
createdAt: 1_663_453_625_129, | ||
userId: '1', | ||
value: [{ children: [{ text: 'This is a comment.' }], type: 'p' }], | ||
}, | ||
2: { | ||
id: '2', | ||
createdAt: 1_663_453_729_191, | ||
userId: '1', | ||
value: [ | ||
{ children: [{ text: 'Can you review this one @12joan?' }], type: 'p' }, | ||
], | ||
}, | ||
3: { | ||
id: '3', | ||
createdAt: 1_663_453_740_180, | ||
isResolved: true, | ||
userId: '1', | ||
value: [{ children: [{ text: 'This is a resolved comment.' }], type: 'p' }], | ||
}, | ||
4: { | ||
id: '4', | ||
createdAt: 1_663_453_740_181, | ||
parentId: '2', | ||
userId: '2', | ||
value: [{ children: [{ text: 'LGTM.' }], type: 'p' }], | ||
}, | ||
5: { | ||
id: '4', | ||
createdAt: 1_663_453_740_182, | ||
parentId: '2', | ||
userId: '1', | ||
value: [{ children: [{ text: 'Thanks!' }], type: 'p' }], | ||
}, | ||
}; | ||
|
||
export const commentsPlugin = CommentsPlugin.configure({ | ||
options: { | ||
comments: commentsData, | ||
myUserId: '1', | ||
users: { | ||
1: { | ||
id: '1', | ||
avatarUrl: 'https://avatars.githubusercontent.com/u/19695832?s=96&v=4', | ||
name: 'zbeyens', | ||
}, | ||
2: { | ||
id: '2', | ||
avatarUrl: 'https://avatars.githubusercontent.com/u/4272090?v=4', | ||
name: '12joan', | ||
import type { ExtendConfig, Path } from '@udecode/plate'; | ||
|
||
import { isSlateString } from '@udecode/plate'; | ||
import { | ||
type BaseCommentsConfig, | ||
BaseCommentsPlugin, | ||
} from '@udecode/plate-comments'; | ||
import { toTPlatePlugin, useHotkeys } from '@udecode/plate/react'; | ||
|
||
export type CommentsConfig = ExtendConfig< | ||
BaseCommentsConfig, | ||
{ | ||
activeId: string | null; | ||
commentingBlock: Path | null; | ||
hotkey: string[]; | ||
hoverId: string | null; | ||
uniquePathMap: Map<string, Path>; | ||
} | ||
>; | ||
|
||
export const commentsPlugin = toTPlatePlugin<CommentsConfig>( | ||
BaseCommentsPlugin, | ||
{ | ||
handlers: { | ||
onClick: ({ api, event, setOption, type }) => { | ||
let leaf = event.target as HTMLElement; | ||
let isSet = false; | ||
|
||
const unsetActiveSuggestion = () => { | ||
setOption('activeId', null); | ||
isSet = true; | ||
}; | ||
|
||
if (!isSlateString(leaf)) unsetActiveSuggestion(); | ||
|
||
while (leaf.parentElement) { | ||
if (leaf.classList.contains(`slate-${type}`)) { | ||
const commentsEntry = api.comment!.node(); | ||
|
||
if (!commentsEntry) { | ||
unsetActiveSuggestion(); | ||
|
||
break; | ||
} | ||
|
||
const id = api.comment!.nodeId(commentsEntry[0]); | ||
|
||
setOption('activeId', id ?? null); | ||
isSet = true; | ||
|
||
break; | ||
} | ||
|
||
leaf = leaf.parentElement; | ||
} | ||
|
||
if (!isSet) unsetActiveSuggestion(); | ||
}, | ||
}, | ||
}, | ||
render: { afterEditable: () => <CommentsPopover /> }, | ||
}); | ||
options: { | ||
activeId: null, | ||
commentingBlock: null, | ||
hotkey: ['meta+shift+m', 'ctrl+shift+m'], | ||
hoverId: null, | ||
uniquePathMap: new Map(), | ||
}, | ||
useHooks: ({ editor, getOptions }) => { | ||
const { hotkey } = getOptions(); | ||
useHotkeys( | ||
hotkey!, | ||
(e) => { | ||
if (!editor.selection) return; | ||
|
||
e.preventDefault(); | ||
|
||
if (!editor.api.isExpanded()) return; | ||
}, | ||
{ | ||
enableOnContentEditable: true, | ||
} | ||
); | ||
}, | ||
} | ||
); |
This file contains 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
12 changes: 12 additions & 0 deletions
12
templates/plate-playground-template/src/components/editor/plugins/skip-mark-plugin.ts
This file contains 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,12 @@ | ||
'use client'; | ||
import { CodePlugin, SkipMarkPlugin } from '@udecode/plate-basic-marks/react'; | ||
import { CommentsPlugin } from '@udecode/plate-comments/react'; | ||
import { SuggestionPlugin } from '@udecode/plate-suggestion/react'; | ||
|
||
export const skipMarkPlugin = SkipMarkPlugin.configure({ | ||
options: { | ||
query: { | ||
allow: [SuggestionPlugin.key, CodePlugin.key, CommentsPlugin.key], | ||
}, | ||
}, | ||
}); |
90 changes: 90 additions & 0 deletions
90
templates/plate-playground-template/src/components/editor/plugins/suggestion-plugin.tsx
This file contains 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,90 @@ | ||
'use client'; | ||
|
||
import { | ||
type ExtendConfig, | ||
type Path, | ||
isSlateEditor, | ||
isSlateElement, | ||
isSlateString, | ||
} from '@udecode/plate'; | ||
import { | ||
type BaseSuggestionConfig, | ||
BaseSuggestionPlugin, | ||
} from '@udecode/plate-suggestion'; | ||
import { toTPlatePlugin } from '@udecode/plate/react'; | ||
|
||
import { BlockSuggestion } from '@/components/plate-ui/block-suggestion'; | ||
|
||
export type SuggestionConfig = ExtendConfig< | ||
BaseSuggestionConfig, | ||
{ | ||
activeId: string | null; | ||
currentUserId: string; | ||
hoverId: string | null; | ||
uniquePathMap: Map<string, Path>; | ||
} | ||
>; | ||
|
||
export const suggestionPlugin = toTPlatePlugin<SuggestionConfig>( | ||
BaseSuggestionPlugin, | ||
{ | ||
handlers: { | ||
// unset active suggestion when clicking outside of suggestion | ||
onClick: ({ api, event, setOption, type }) => { | ||
let leaf = event.target as HTMLElement; | ||
let isSet = false; | ||
|
||
const unsetActiveSuggestion = () => { | ||
setOption('activeId', null); | ||
isSet = true; | ||
}; | ||
|
||
if (!isSlateString(leaf)) unsetActiveSuggestion(); | ||
|
||
while ( | ||
leaf.parentElement && | ||
!isSlateElement(leaf.parentElement) && | ||
!isSlateEditor(leaf.parentElement) | ||
) { | ||
if (leaf.classList.contains(`slate-${type}`)) { | ||
const suggestionEntry = api.suggestion!.node({ | ||
isText: true, | ||
}); | ||
|
||
if (!suggestionEntry) { | ||
unsetActiveSuggestion(); | ||
|
||
break; | ||
} | ||
|
||
const id = api.suggestion!.nodeId(suggestionEntry[0]); | ||
|
||
setOption('activeId', id ?? null); | ||
isSet = true; | ||
|
||
break; | ||
} | ||
|
||
leaf = leaf.parentElement; | ||
} | ||
|
||
if (!isSet) unsetActiveSuggestion(); | ||
}, | ||
}, | ||
options: { | ||
activeId: null, | ||
currentUserId: 'user3', | ||
hoverId: null, | ||
uniquePathMap: new Map(), | ||
}, | ||
render: { | ||
belowRootNodes: ({ api, element }) => { | ||
if (!api.suggestion!.isBlockSuggestion(element)) { | ||
return null; | ||
} | ||
|
||
return <BlockSuggestion element={element} />; | ||
}, | ||
}, | ||
} | ||
); |
Oops, something went wrong.