-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix project data and get code to editor
- Loading branch information
1 parent
7fb76a6
commit f8f466b
Showing
11 changed files
with
176 additions
and
129 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
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
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
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,23 +1,6 @@ | ||
import { MyValue, createMyPlugins } from "@editor/types"; | ||
import { PlateEditor, createPlateEditor } from "@udecode/plate-common"; | ||
import { MyValue, } from "@editor/types"; | ||
import { WritableAtom, atom } from "jotai"; | ||
|
||
// plugins | ||
import { basicNodesPlugins } from "./basicNodesPlugin"; | ||
import { createClipboardPlugin } from "@editor/clipboard/createClipboardPlugin"; | ||
|
||
export const plugins = createMyPlugins( | ||
[ | ||
...basicNodesPlugins, | ||
createClipboardPlugin(), | ||
] | ||
); | ||
|
||
export const editorAtom = atom<PlateEditor<MyValue>>( | ||
createPlateEditor({ | ||
id: '', | ||
plugins: plugins | ||
})) | ||
|
||
export const changeAtom = atom<MyValue>(null) as WritableAtom<MyValue, [MyValue], void> | ||
export const selectionAtom = atom<object>({}) as WritableAtom<object, [object], void> |
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,10 @@ | ||
import { createMyPlugins } from "@editor/types"; | ||
import { basicNodesPlugins } from "./basicNodesPlugin"; | ||
import { createClipboardPlugin } from "@editor/clipboard/createClipboardPlugin"; | ||
|
||
export const plugins = createMyPlugins( | ||
[ | ||
...basicNodesPlugins, | ||
createClipboardPlugin(), | ||
] | ||
); |
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,62 @@ | ||
import { codeAtom, editorServiceAtom, savingServiceAtom } from "@core/atoms/smith" | ||
import { plugins } from "@editor/common/plugins" | ||
import { deserializeCode, serializeCode } from "@editor/serializers/serializers" | ||
import { MyValue } from "@editor/types" | ||
import { PlateEditor, PlateProvider } from "@udecode/plate-common" | ||
import { useAtomValue, useSetAtom } from "jotai" | ||
import { useCallback, useMemo, useRef } from "react" | ||
import { debounce } from "lodash" | ||
import { changeAtom, selectionAtom } from "@editor/common/atoms" | ||
|
||
export const EditorProvider = ({ children }) => { | ||
const code = useAtomValue(codeAtom) | ||
const editorRef = useRef<PlateEditor<MyValue> | null>(null); | ||
|
||
// machines | ||
const sendEditor = useSetAtom(editorServiceAtom) | ||
const sendSave = useSetAtom(savingServiceAtom) | ||
|
||
// FSM actions | ||
const setSelection = useSetAtom(selectionAtom) | ||
const setChange = useSetAtom(changeAtom) | ||
|
||
|
||
const saveCode = debounce((value) => { | ||
// console.log("code") | ||
const code = serializeCode(value) | ||
sendEditor({ type: "CODE", value: code }) | ||
if (code != '') { | ||
sendSave({ type: 'SAVE' }) | ||
} | ||
}, 1000) | ||
|
||
const onEditorChanged = useCallback( | ||
(value: MyValue) => { | ||
const editor = editorRef.current; | ||
// setChange(editor.operations) | ||
const isAstChange = editor.operations.some( | ||
op => 'set_selection' !== op.type | ||
) | ||
// console.log(editor.operations) | ||
if (isAstChange) { | ||
// console.log('changed') | ||
// Serialize the value and save the string value | ||
saveCode(value) | ||
} else { | ||
setSelection({}) | ||
} | ||
}, []) | ||
|
||
const editorValue = useMemo(() => deserializeCode(code), [code]) | ||
|
||
return <PlateProvider<MyValue> | ||
// id={id} | ||
editorRef={editorRef} | ||
plugins={plugins} | ||
value={editorValue} | ||
onChange={onEditorChanged} | ||
> | ||
{children} | ||
</PlateProvider> | ||
|
||
} |
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
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
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
Oops, something went wrong.