Skip to content
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

Move @workduck-io/mex-editor inside Extension #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@sentry/react": "^6.17.2",
"@vespaiach/axios-fetch-adapter": "^0.1.1",
"@workduck-io/dwindle": "^0.0.15",
"@workduck-io/mex-editor": "^0.5.0",
"chrome-extension-async": "^3.4.1",
"core-js": "^3.21.1",
"crypto-browserify": "^3.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { TEditor, getNodes, getSelectionText, insertNodes } from '@udecode/plate

import genereateName from 'project-name-generator'
import toast from 'react-hot-toast'
import { ELEMENT_ILINK, ILinkNode } from '@workduck-io/mex-editor'
import { convertContentToRawText } from '@mexit/core'
import { generateSnippetId, mog } from '@mexit/core'
import { useContentStore } from '../../../../Stores/useContentStore'
import { useSnippetStore } from '../../../../Stores/useSnippetStore'

export const useTransform = () => {
// const addILink = useDataStore((s) => s.addILink)
const addSnippet = useSnippetStore((s) => s.addSnippet)
const setContent = useContentStore((s) => s.setContent)

const convertSelectionToQABlock = (editor: TEditor) => {
try {
Expand Down Expand Up @@ -47,32 +44,6 @@ export const useTransform = () => {
}, false)
}

const replaceSelectionWithLink = (editor: TEditor, ilink: string, inline: boolean) => {
try {
const selectionPath = Editor.path(editor, editor.selection)

// mog('replaceSelectionWithLink selPath', { selectionPath })

if (inline) Transforms.delete(editor)
else
Transforms.removeNodes(editor, {
at: editor.selection,
hanging: false
})
// Transforms.liftNodes(editor, { at: editor.selection, mode: 'lowest' })

// mog('replaceSelectionWithLink detFrag', { selectionPath })

insertNodes<ILinkNode>(editor, [{ type: ELEMENT_ILINK, value: ilink, children: [] }], {
at: editor.selection
})
// mog('replaceSelectionWithLink insNode', { sel: editor.selection })
} catch (e) {
console.error(e)
return e
}
}

/**
* Converts selection to new snippet
* Shows notification of snippet creation
Expand Down Expand Up @@ -155,7 +126,6 @@ export const useTransform = () => {
if (!isConvertable(editor)) return

Editor.withoutNormalizing(editor, () => {
const selectionPath = Editor.path(editor, editor.selection)
const nodes = Array.from(
getNodes(editor, {
mode: 'highest',
Expand Down
17 changes: 9 additions & 8 deletions apps/extension/src/Components/Editor/Components.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { createPlateUI, withProps } from '@udecode/plate'
import { StyledElement } from '@udecode/plate-styled-components'
import { MediaEmbedElement, TableWrapper, LinkElement } from '@mexit/shared'
import TagElement from '../../Editor/components/Tags/TagElement'
import QuickLinkElement from '../../Editor/plugins/QuickLink/components/QuickLinkElement'
import {
createPlateUI,
ELEMENT_ILINK,
ELEMENT_LINK,
ELEMENT_MEDIA_EMBED,
ELEMENT_PARAGRAPH,
ELEMENT_TABLE,
withProps
} from '@udecode/plate'
import { StyledElement } from '@udecode/plate-styled-components'
import { TagElement } from '@workduck-io/mex-editor'
import { MediaEmbedElement, TableWrapper, LinkElement } from '@mexit/shared'

export const ELEMENT_TAG = 'tag'
ELEMENT_TAG
} from '@mexit/core'

export const components = {
[ELEMENT_LINK]: withProps(LinkElement, {
Expand All @@ -26,6 +26,7 @@ export const components = {
}
}),
[ELEMENT_TAG]: TagElement,
[ELEMENT_ILINK]: QuickLinkElement,
[ELEMENT_MEDIA_EMBED]: MediaEmbedElement,
[ELEMENT_TABLE]: TableWrapper
}
Expand Down
33 changes: 6 additions & 27 deletions apps/extension/src/Components/Editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { createPlugins, ELEMENT_MEDIA_EMBED, ELEMENT_TABLE } from '@udecode/plate'
import {
MexEditor,
ComboboxKey,
QuickLinkElement,
ComboboxConfig,
ELEMENT_TAG,
ELEMENT_ILINK
} from '@workduck-io/mex-editor'
import { useSpring } from 'react-spring'
import { useDebouncedCallback } from 'use-debounce'

import React, { useState, useEffect, useMemo, useRef } from 'react'
import styled from 'styled-components'
import React, { useState, useMemo } from 'react'

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

import components from './Components'
import BallonMarkToolbarButtons from './BalloonToolbar/EditorBalloonToolbar'
import { Tag, CaptureType, QuickLinkType, ActionType } from '@mexit/core'
import { Tag, QuickLinkType, ActionType, ELEMENT_TAG, ELEMENT_ILINK } from '@mexit/core'
import { useEditorContext } from '../../Hooks/useEditorContext'
import { styleSlot } from '../../contentScript'
import useDataStore from '../../Stores/useDataStore'
import { MexEditorOptions } from '@workduck-io/mex-editor/lib/types/editor'
import { ComboboxConfig, ComboboxKey } from '../../Editor/types'
import MexEditor from '../../Editor/MexEditor'
import { MexEditorOptions } from '../../Editor/types/editor'

interface EditorProps {
nodePath?: string
Expand All @@ -43,12 +35,6 @@ const commands = [
icon: 'ri:table-line',
type: 'Quick Actions'
},
// {
// command: 'canvas',
// text: 'Insert Drawing canvas',
// icon: 'ri:markup-line',
// type: 'Quick Actions'
// },
{
command: 'webem',
text: 'Insert Web embed',
Expand All @@ -60,18 +46,11 @@ const commands = [
export const Editor: React.FC<EditorProps> = ({ readOnly, onChange }) => {
const { searchResults, activeIndex, activeItem } = useSputlitContext()
const { previewMode, nodeContent, node, setPreviewMode } = useEditorContext()
const currTabURL = window.location.href
const [pageMetaTags, setPageMetaTags] = useState<any[]>([])
const [userTags, setUserTags] = useState<Tag[]>([])
const ilinks = useDataStore((store) => store.ilinks)

const addTags = useTagStore((store) => store.addTags)
const tags = useTagStore((store) => store.tags)

const plugins = createPlugins(generatePlugins())
const userDetails = useAuthStore((store) => store.userDetails)
const workspaceDetails = useAuthStore((store) => store.workspaceDetails)

useEditorChange(node.nodeid, nodeContent, onChange)

const comboboxConfig: ComboboxConfig = {
Expand Down Expand Up @@ -183,7 +162,7 @@ export const Editor: React.FC<EditorProps> = ({ readOnly, onChange }) => {
options={editorOptions}
editorId={node.nodeid}
value={nodeContent}
portalElement={document.getElementById('mexit').shadowRoot.getElementById('sputlit-main')}
portalElement={document.getElementById('mexit').shadowRoot.getElementById('sputlit-container')}
/>
</EditorStyles>
</EditorWrapper>
Expand Down
57 changes: 57 additions & 0 deletions apps/extension/src/Editor/MexEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useState, useEffect } from 'react'
import { Plate, selectEditor, usePlateEditorRef } from '@udecode/plate'
import { useComboboxConfig } from './components/ComboBox/config'
import { MultiComboboxContainer } from './components/MultiCombobox/multiComboboxContainer'
import { useMexEditorStore } from './store/editor'
import { MexEditorProps, MexEditorValue } from './types/editor'

export function MexEditor(props: MexEditorProps) {
const editorRef = usePlateEditorRef()
const [content, setContent] = useState<MexEditorValue>([])
const setMetaData = useMexEditorStore((s) => s.setMetaData)

useEffect(() => {
if (editorRef && props?.options?.focusOptions) {
selectEditor(editorRef, props.options.focusOptions)
}
setMetaData(props.meta)
}, [editorRef, props.editorId]) // eslint-disable-line

const { plugins, comboOnKeydownConfig } = useComboboxConfig(
props.editorId,
props?.comboboxConfig,
props?.components,
props?.plugins
)

const onChange = (value: MexEditorValue) => {
setContent(value)
if (props.onChange) {
props.onChange(value)
}
}

return (
<>
<Plate
id={props.editorId}
value={props.value}
onChange={onChange}
editableProps={props?.options?.editableProps}
plugins={plugins}
>
<>
<MultiComboboxContainer
keys={comboOnKeydownConfig.keys}
slashCommands={comboOnKeydownConfig.slashCommands}
portalElement={props?.portalElement}
/>
{props.options?.withBalloonToolbar && props.BalloonMarkToolbarButtons}
</>
</Plate>
{props.debug && <pre>{JSON.stringify(content, null, 2)}</pre>}
</>
)
}

export default MexEditor
78 changes: 78 additions & 0 deletions apps/extension/src/Editor/components/ComboBox/config.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { PlatePlugin, PlatePluginComponent } from '@udecode/plate'
import useMemoizedPlugins, { generatePlugins } from '../../plugins'
import { QuickLinkComboboxItem } from '../../plugins/QuickLink/components/QuickLinkComboboxItem'
import { ELEMENT_ILINK, ELEMENT_TAG } from '@mexit/core'
import useMultiComboboxOnChange from '../MultiCombobox/useMultiComboboxChange'
import useMultiComboboxOnKeyDown from '../MultiCombobox/useMultiComboboxOnKeyDown'
import { SlashComboboxItem } from '../SlashCommands/SlashComboboxItem'
import { TagComboboxItem } from '../Tags/TagComboboxItem'
import { ComboboxConfig, ComboboxKey, ComboboxKeyDownConfig, ComboboxOnChangeConfig } from './types'

export const useComboboxConfig = (
editorId: string,
config: ComboboxConfig,
components: Record<string, PlatePluginComponent<any | undefined>> = {},
customPlugins?: Array<PlatePlugin>
) => {
const keys = config.onKeyDownConfig.keys

const comboOnKeydownConfig: ComboboxKeyDownConfig = {
keys: {
ilink: {
slateElementType: ELEMENT_ILINK,
newItemHandler: keys.ilink.newItemHandler,
itemRenderer: QuickLinkComboboxItem
},
tag: {
slateElementType: ELEMENT_TAG,
newItemHandler: keys.tag.newItemHandler,
itemRenderer: TagComboboxItem
},
slash_command: {
slateElementType: 'slash_command',
newItemHandler: keys.slash_command.newItemHandler,
itemRenderer: SlashComboboxItem
}
},
slashCommands: config.onKeyDownConfig.slashCommands
}

const comboOnChangeConfig: ComboboxOnChangeConfig = {
ilink: {
cbKey: ComboboxKey.ILINK,
trigger: '[[',
data: []
},
tag: {
cbKey: ComboboxKey.TAG,
trigger: '#',
data: [],
icon: 'ri:hashtag'
},
slash_command: {
cbKey: ComboboxKey.SLASH_COMMAND,
trigger: '/',
icon: 'ri:flask-line',
data: []
},
...(config.onChangeConfig as any)
}

const prePlugins = useMemoizedPlugins(customPlugins ?? generatePlugins(), components)

const plugins = [
...prePlugins,
{
key: 'MULTI_COMBOBOX',
handlers: {
onChange: useMultiComboboxOnChange(editorId, comboOnChangeConfig),
onKeyDown: useMultiComboboxOnKeyDown(comboOnKeydownConfig)
}
}
]

return {
plugins,
comboOnKeydownConfig
}
}
Loading