Skip to content

Commit a44025e

Browse files
Mukul Mehtasahil-shubham
Mukul Mehta
andauthored
Move @workduck-io/mex-editor inside Extension (#77)
#77 * Use chromeStorageAdapter from inside of extension for stores * mex-space editor inside extension now, with new useElementOnChange hook * TagComponent in extension editor now, tsc --noEmit generates no errors * Remove references to @workduck-io/mex-editor inside extension * Remove redundant useHotKeys hook call * Remove redundant useOnMouseClick hook call * fixed extension quicklink element; removed duplicate code; Signed-off-by: Sahil Shubham <[email protected]> * fixed editor components positioning; Signed-off-by: Sahil Shubham <[email protected]> Co-authored-by: Sahil Shubham <[email protected]>
1 parent 89e8565 commit a44025e

File tree

96 files changed

+3343
-625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+3343
-625
lines changed

apps/extension/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"@sentry/react": "^6.17.2",
1818
"@vespaiach/axios-fetch-adapter": "^0.1.1",
1919
"@workduck-io/dwindle": "^0.0.15",
20-
"@workduck-io/mex-editor": "^0.5.0",
2120
"chrome-extension-async": "^3.4.1",
2221
"core-js": "^3.21.1",
2322
"crypto-browserify": "^3.12.0",

apps/extension/src/Components/Editor/BalloonToolbar/components/useTransform.ts

-30
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import { TEditor, getNodes, getSelectionText, insertNodes } from '@udecode/plate
33

44
import genereateName from 'project-name-generator'
55
import toast from 'react-hot-toast'
6-
import { ELEMENT_ILINK, ILinkNode } from '@workduck-io/mex-editor'
76
import { convertContentToRawText } from '@mexit/core'
87
import { generateSnippetId, mog } from '@mexit/core'
98
import { useContentStore } from '../../../../Stores/useContentStore'
109
import { useSnippetStore } from '../../../../Stores/useSnippetStore'
1110

1211
export const useTransform = () => {
13-
// const addILink = useDataStore((s) => s.addILink)
1412
const addSnippet = useSnippetStore((s) => s.addSnippet)
15-
const setContent = useContentStore((s) => s.setContent)
1613

1714
const convertSelectionToQABlock = (editor: TEditor) => {
1815
try {
@@ -47,32 +44,6 @@ export const useTransform = () => {
4744
}, false)
4845
}
4946

50-
const replaceSelectionWithLink = (editor: TEditor, ilink: string, inline: boolean) => {
51-
try {
52-
const selectionPath = Editor.path(editor, editor.selection)
53-
54-
// mog('replaceSelectionWithLink selPath', { selectionPath })
55-
56-
if (inline) Transforms.delete(editor)
57-
else
58-
Transforms.removeNodes(editor, {
59-
at: editor.selection,
60-
hanging: false
61-
})
62-
// Transforms.liftNodes(editor, { at: editor.selection, mode: 'lowest' })
63-
64-
// mog('replaceSelectionWithLink detFrag', { selectionPath })
65-
66-
insertNodes<ILinkNode>(editor, [{ type: ELEMENT_ILINK, value: ilink, children: [] }], {
67-
at: editor.selection
68-
})
69-
// mog('replaceSelectionWithLink insNode', { sel: editor.selection })
70-
} catch (e) {
71-
console.error(e)
72-
return e
73-
}
74-
}
75-
7647
/**
7748
* Converts selection to new snippet
7849
* Shows notification of snippet creation
@@ -155,7 +126,6 @@ export const useTransform = () => {
155126
if (!isConvertable(editor)) return
156127

157128
Editor.withoutNormalizing(editor, () => {
158-
const selectionPath = Editor.path(editor, editor.selection)
159129
const nodes = Array.from(
160130
getNodes(editor, {
161131
mode: 'highest',

apps/extension/src/Components/Editor/Components.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3+
import { createPlateUI, withProps } from '@udecode/plate'
4+
import { StyledElement } from '@udecode/plate-styled-components'
5+
import { MediaEmbedElement, TableWrapper, LinkElement } from '@mexit/shared'
6+
import TagElement from '../../Editor/components/Tags/TagElement'
7+
import QuickLinkElement from '../../Editor/plugins/QuickLink/components/QuickLinkElement'
38
import {
4-
createPlateUI,
9+
ELEMENT_ILINK,
510
ELEMENT_LINK,
611
ELEMENT_MEDIA_EMBED,
712
ELEMENT_PARAGRAPH,
813
ELEMENT_TABLE,
9-
withProps
10-
} from '@udecode/plate'
11-
import { StyledElement } from '@udecode/plate-styled-components'
12-
import { TagElement } from '@workduck-io/mex-editor'
13-
import { MediaEmbedElement, TableWrapper, LinkElement } from '@mexit/shared'
14-
15-
export const ELEMENT_TAG = 'tag'
14+
ELEMENT_TAG
15+
} from '@mexit/core'
1616

1717
export const components = {
1818
[ELEMENT_LINK]: withProps(LinkElement, {
@@ -26,6 +26,7 @@ export const components = {
2626
}
2727
}),
2828
[ELEMENT_TAG]: TagElement,
29+
[ELEMENT_ILINK]: QuickLinkElement,
2930
[ELEMENT_MEDIA_EMBED]: MediaEmbedElement,
3031
[ELEMENT_TABLE]: TableWrapper
3132
}

apps/extension/src/Components/Editor/index.tsx

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import { createPlugins, ELEMENT_MEDIA_EMBED, ELEMENT_TABLE } from '@udecode/plate'
2-
import {
3-
MexEditor,
4-
ComboboxKey,
5-
QuickLinkElement,
6-
ComboboxConfig,
7-
ELEMENT_TAG,
8-
ELEMENT_ILINK
9-
} from '@workduck-io/mex-editor'
102
import { useSpring } from 'react-spring'
113
import { useDebouncedCallback } from 'use-debounce'
124

13-
import React, { useState, useEffect, useMemo, useRef } from 'react'
14-
import styled from 'styled-components'
5+
import React, { useState, useMemo } from 'react'
156

167
import { EditorStyles, useEditorChange } from '@mexit/shared'
178
import generatePlugins from '../../Utils/plugins'
@@ -22,11 +13,12 @@ import { useTagStore } from '../../Hooks/useTags'
2213

2314
import components from './Components'
2415
import BallonMarkToolbarButtons from './BalloonToolbar/EditorBalloonToolbar'
25-
import { Tag, CaptureType, QuickLinkType, ActionType } from '@mexit/core'
16+
import { Tag, QuickLinkType, ActionType, ELEMENT_TAG, ELEMENT_ILINK } from '@mexit/core'
2617
import { useEditorContext } from '../../Hooks/useEditorContext'
27-
import { styleSlot } from '../../contentScript'
2818
import useDataStore from '../../Stores/useDataStore'
29-
import { MexEditorOptions } from '@workduck-io/mex-editor/lib/types/editor'
19+
import { ComboboxConfig, ComboboxKey } from '../../Editor/types'
20+
import MexEditor from '../../Editor/MexEditor'
21+
import { MexEditorOptions } from '../../Editor/types/editor'
3022

3123
interface EditorProps {
3224
nodePath?: string
@@ -43,12 +35,6 @@ const commands = [
4335
icon: 'ri:table-line',
4436
type: 'Quick Actions'
4537
},
46-
// {
47-
// command: 'canvas',
48-
// text: 'Insert Drawing canvas',
49-
// icon: 'ri:markup-line',
50-
// type: 'Quick Actions'
51-
// },
5238
{
5339
command: 'webem',
5440
text: 'Insert Web embed',
@@ -60,18 +46,11 @@ const commands = [
6046
export const Editor: React.FC<EditorProps> = ({ readOnly, onChange }) => {
6147
const { searchResults, activeIndex, activeItem } = useSputlitContext()
6248
const { previewMode, nodeContent, node, setPreviewMode } = useEditorContext()
63-
const currTabURL = window.location.href
64-
const [pageMetaTags, setPageMetaTags] = useState<any[]>([])
65-
const [userTags, setUserTags] = useState<Tag[]>([])
6649
const ilinks = useDataStore((store) => store.ilinks)
6750

6851
const addTags = useTagStore((store) => store.addTags)
6952
const tags = useTagStore((store) => store.tags)
7053

71-
const plugins = createPlugins(generatePlugins())
72-
const userDetails = useAuthStore((store) => store.userDetails)
73-
const workspaceDetails = useAuthStore((store) => store.workspaceDetails)
74-
7554
useEditorChange(node.nodeid, nodeContent, onChange)
7655

7756
const comboboxConfig: ComboboxConfig = {
@@ -183,7 +162,7 @@ export const Editor: React.FC<EditorProps> = ({ readOnly, onChange }) => {
183162
options={editorOptions}
184163
editorId={node.nodeid}
185164
value={nodeContent}
186-
portalElement={document.getElementById('mexit').shadowRoot.getElementById('sputlit-main')}
165+
portalElement={document.getElementById('mexit').shadowRoot.getElementById('sputlit-container')}
187166
/>
188167
</EditorStyles>
189168
</EditorWrapper>
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React, { useState, useEffect } from 'react'
2+
import { Plate, selectEditor, usePlateEditorRef } from '@udecode/plate'
3+
import { useComboboxConfig } from './components/ComboBox/config'
4+
import { MultiComboboxContainer } from './components/MultiCombobox/multiComboboxContainer'
5+
import { useMexEditorStore } from './store/editor'
6+
import { MexEditorProps, MexEditorValue } from './types/editor'
7+
8+
export function MexEditor(props: MexEditorProps) {
9+
const editorRef = usePlateEditorRef()
10+
const [content, setContent] = useState<MexEditorValue>([])
11+
const setMetaData = useMexEditorStore((s) => s.setMetaData)
12+
13+
useEffect(() => {
14+
if (editorRef && props?.options?.focusOptions) {
15+
selectEditor(editorRef, props.options.focusOptions)
16+
}
17+
setMetaData(props.meta)
18+
}, [editorRef, props.editorId]) // eslint-disable-line
19+
20+
const { plugins, comboOnKeydownConfig } = useComboboxConfig(
21+
props.editorId,
22+
props?.comboboxConfig,
23+
props?.components,
24+
props?.plugins
25+
)
26+
27+
const onChange = (value: MexEditorValue) => {
28+
setContent(value)
29+
if (props.onChange) {
30+
props.onChange(value)
31+
}
32+
}
33+
34+
return (
35+
<>
36+
<Plate
37+
id={props.editorId}
38+
value={props.value}
39+
onChange={onChange}
40+
editableProps={props?.options?.editableProps}
41+
plugins={plugins}
42+
>
43+
<>
44+
<MultiComboboxContainer
45+
keys={comboOnKeydownConfig.keys}
46+
slashCommands={comboOnKeydownConfig.slashCommands}
47+
portalElement={props?.portalElement}
48+
/>
49+
{props.options?.withBalloonToolbar && props.BalloonMarkToolbarButtons}
50+
</>
51+
</Plate>
52+
{props.debug && <pre>{JSON.stringify(content, null, 2)}</pre>}
53+
</>
54+
)
55+
}
56+
57+
export default MexEditor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { PlatePlugin, PlatePluginComponent } from '@udecode/plate'
2+
import useMemoizedPlugins, { generatePlugins } from '../../plugins'
3+
import { QuickLinkComboboxItem } from '../../plugins/QuickLink/components/QuickLinkComboboxItem'
4+
import { ELEMENT_ILINK, ELEMENT_TAG } from '@mexit/core'
5+
import useMultiComboboxOnChange from '../MultiCombobox/useMultiComboboxChange'
6+
import useMultiComboboxOnKeyDown from '../MultiCombobox/useMultiComboboxOnKeyDown'
7+
import { SlashComboboxItem } from '../SlashCommands/SlashComboboxItem'
8+
import { TagComboboxItem } from '../Tags/TagComboboxItem'
9+
import { ComboboxConfig, ComboboxKey, ComboboxKeyDownConfig, ComboboxOnChangeConfig } from './types'
10+
11+
export const useComboboxConfig = (
12+
editorId: string,
13+
config: ComboboxConfig,
14+
components: Record<string, PlatePluginComponent<any | undefined>> = {},
15+
customPlugins?: Array<PlatePlugin>
16+
) => {
17+
const keys = config.onKeyDownConfig.keys
18+
19+
const comboOnKeydownConfig: ComboboxKeyDownConfig = {
20+
keys: {
21+
ilink: {
22+
slateElementType: ELEMENT_ILINK,
23+
newItemHandler: keys.ilink.newItemHandler,
24+
itemRenderer: QuickLinkComboboxItem
25+
},
26+
tag: {
27+
slateElementType: ELEMENT_TAG,
28+
newItemHandler: keys.tag.newItemHandler,
29+
itemRenderer: TagComboboxItem
30+
},
31+
slash_command: {
32+
slateElementType: 'slash_command',
33+
newItemHandler: keys.slash_command.newItemHandler,
34+
itemRenderer: SlashComboboxItem
35+
}
36+
},
37+
slashCommands: config.onKeyDownConfig.slashCommands
38+
}
39+
40+
const comboOnChangeConfig: ComboboxOnChangeConfig = {
41+
ilink: {
42+
cbKey: ComboboxKey.ILINK,
43+
trigger: '[[',
44+
data: []
45+
},
46+
tag: {
47+
cbKey: ComboboxKey.TAG,
48+
trigger: '#',
49+
data: [],
50+
icon: 'ri:hashtag'
51+
},
52+
slash_command: {
53+
cbKey: ComboboxKey.SLASH_COMMAND,
54+
trigger: '/',
55+
icon: 'ri:flask-line',
56+
data: []
57+
},
58+
...(config.onChangeConfig as any)
59+
}
60+
61+
const prePlugins = useMemoizedPlugins(customPlugins ?? generatePlugins(), components)
62+
63+
const plugins = [
64+
...prePlugins,
65+
{
66+
key: 'MULTI_COMBOBOX',
67+
handlers: {
68+
onChange: useMultiComboboxOnChange(editorId, comboOnChangeConfig),
69+
onKeyDown: useMultiComboboxOnKeyDown(comboOnKeydownConfig)
70+
}
71+
}
72+
]
73+
74+
return {
75+
plugins,
76+
comboOnKeydownConfig
77+
}
78+
}

0 commit comments

Comments
 (0)