Skip to content
Open
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
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build:local": "electron-vite build"
},
"dependencies": {
"@codemirror/commands": "^6.8.1",
"@codemirror/lang-markdown": "^6.3.2",
"@codemirror/language-data": "^6.5.1",
"@codemirror/state": "^6.5.2",
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function App(): JSX.Element {
const [focus, setFocus] = useState<FocusTarget>('fileList')
const [currentListItem, setCurrentListItem] = useState<string | null>(null)
const [current, setCurrent] = useState<string | null>(null)
const [editorExtensions, setEditorExtensions] = useState([])

const toggleFocus = (value): void => {
if (value === focus) {
Expand All @@ -36,7 +37,9 @@ export default function App(): JSX.Element {
isVisible: isEditorVisible,
setIsVisible: setIsEditorVisible,
current,
setCurrent
setCurrent,
setEditorExtensions,
editorExtensions
}}
>
<FocusContext.Provider
Expand Down
79 changes: 43 additions & 36 deletions src/renderer/src/components/BodyField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Props {
window.EditContext = false

export default function BodyField({ value, onChange, onKeyDownCapture }: Props): JSX.Element {
const { bodyEditor, isVisible } = useContext(EditorContext)
const { bodyEditor, isVisible, setEditorExtensions, editorExtensions } = useContext(EditorContext)

const insertImage = useCallback(
(name, pos): void => {
Expand All @@ -40,27 +40,30 @@ export default function BodyField({ value, onChange, onKeyDownCapture }: Props):
[bodyEditor]
)

const handleFileDrop = (e): void => {
e.preventDefault()
const handleFileDrop = useCallback(
(e): void => {
e.preventDefault()

const items = e.dataTransfer!.items!
for (let i = 0; i < items.length; i++) {
const item = items[i].webkitGetAsEntry()
if (item && item.isFile) {
new Promise<File>((resolve, reject) => {
;(item as FileSystemFileEntry).file(resolve, reject)
}).then((file) => {
window.api.copyFile(file).then(() => {
const pos = bodyEditor?.current?.view?.posAtCoords({
x: e.pageX,
y: e.pageY
const items = e.dataTransfer!.items!
for (let i = 0; i < items.length; i++) {
const item = items[i].webkitGetAsEntry()
if (item && item.isFile) {
new Promise<File>((resolve, reject) => {
;(item as FileSystemFileEntry).file(resolve, reject)
}).then((file) => {
window.api.copyFile(file).then(() => {
const pos = bodyEditor?.current?.view?.posAtCoords({
x: e.pageX,
y: e.pageY
})
insertImage(item.name, pos)
})
insertImage(item.name, pos)
})
})
}
}
}
}
},
[bodyEditor, insertImage]
)

const theme = useMemo(() => {
if (
Expand All @@ -78,6 +81,27 @@ export default function BodyField({ value, onChange, onKeyDownCapture }: Props):
return thememirror[window.textZenInternal.config.view.theme!]
}, [])

useEffect(() => {
setEditorExtensions([
markdown({ base: markdownLanguage, codeLanguages: languages }),
hyperLink,
EditorView.lineWrapping,
markdownImagePlugin,
internalLink,
...highlights,
autocompletion({
override: [internalLinkCompletion]
}),
EditorView.domEventHandlers({
drop: handleFileDrop
}),
theme,
mermaidPlugin,
tablePreviewPlugin,
mathFormulaPlugin
])
}, [handleFileDrop, setEditorExtensions, theme])

useEffect(() => {
window.textZen.codemirror.view = bodyEditor?.current?.view
window.textZen.codemirror.state = bodyEditor?.current?.state
Expand All @@ -91,24 +115,7 @@ export default function BodyField({ value, onChange, onKeyDownCapture }: Props):
return (
<CodeMirror
value={value}
extensions={[
markdown({ base: markdownLanguage, codeLanguages: languages }),
hyperLink,
EditorView.lineWrapping,
markdownImagePlugin,
internalLink,
...highlights,
autocompletion({
override: [internalLinkCompletion]
}),
EditorView.domEventHandlers({
drop: handleFileDrop
}),
theme,
mermaidPlugin,
tablePreviewPlugin,
mathFormulaPlugin
]}
extensions={editorExtensions}
onChange={onChange}
ref={bodyEditor}
basicSetup={{
Expand Down
38 changes: 25 additions & 13 deletions src/renderer/src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { File } from './Page'
import { useDebouncedCallback } from 'use-debounce'
import { EditorContext } from '@renderer/contexts/editorContext'
import { FocusContext } from '@renderer/contexts/FocusContext'
import { EditorState } from '@codemirror/state'
import { historyField } from '@codemirror/commands'

interface Props {
currentFile: File
Expand All @@ -23,9 +25,8 @@ export default function Editor({
currentTitle,
onBodyChange
}: Props): JSX.Element {
const [editorVisible, setEditorVisible] = useState(true)
const [currentBody, setCurrentBody] = useState('')
const { bodyEditor, titleEditor } = useContext(EditorContext)
const { bodyEditor, titleEditor, editorExtensions } = useContext(EditorContext)
const { setFocus } = useContext(FocusContext)

const writeFile = useDebouncedCallback(async (t, b, target) => {
Expand Down Expand Up @@ -115,15 +116,28 @@ export default function Editor({
}
}

// エディタの編集履歴をリセットする
useEffect(() => {
if (!currentFile) {
return
}
window.api.getBody(currentFile.id).then(setCurrentBody)
setEditorVisible(false)
setTimeout(() => setEditorVisible(true), 1)
}, [currentFile, setCurrentTitle])

if (!bodyEditor?.current?.view?.state) {
return
}

bodyEditor?.current?.view?.setState(
EditorState.fromJSON(
{
...bodyEditor?.current?.view.state.toJSON({ history: historyField }),
history
},
{
extensions: editorExtensions
}
)
)
}, [bodyEditor, currentFile, setCurrentTitle])

useEffect(() => {
setCurrentTitle(currentFile.title)
Expand All @@ -136,13 +150,11 @@ export default function Editor({
onChange={handleTitleChange}
onKeyDown={handleTitleKeyDown}
/>
{editorVisible && (
<BodyField
value={currentBody}
onChange={handleUpdate}
onKeyDownCapture={handleBodyKeyDownCapture as (keyboardEvent) => void}
/>
)}
<BodyField
value={currentBody}
onChange={handleUpdate}
onKeyDownCapture={handleBodyKeyDownCapture as (keyboardEvent) => void}
/>
</div>
)
}
8 changes: 6 additions & 2 deletions src/renderer/src/contexts/editorContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactCodeMirrorRef } from '@uiw/react-codemirror'
import { Extension, ReactCodeMirrorRef } from '@uiw/react-codemirror'
import { createContext, Dispatch, SetStateAction } from 'react'

interface ContextType {
Expand All @@ -8,6 +8,8 @@ interface ContextType {
setIsVisible: (value: boolean) => void
current: string | null
setCurrent: Dispatch<SetStateAction<string | null>>
setEditorExtensions: (Extension) => void
editorExtensions: Extension[]
}

export const EditorContext = createContext<ContextType>({
Expand All @@ -16,5 +18,7 @@ export const EditorContext = createContext<ContextType>({
isVisible: true,
setIsVisible: () => {},
current: null,
setCurrent: () => {}
setCurrent: () => {},
setEditorExtensions: () => {},
editorExtensions: []
})
Loading