Component cleanup
This commit is contained in:
parent
2bc8415c51
commit
03568357ba
1 changed files with 15 additions and 13 deletions
|
|
@ -44,25 +44,13 @@ const Editor = ({
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const locale = router.locale || 'en'
|
const locale = router.locale || 'en'
|
||||||
|
|
||||||
// const [editor, setEditor] = useState<TiptapEditor | undefined>(undefined)
|
|
||||||
|
|
||||||
function isJSON(content?: string) {
|
|
||||||
if (!content) return false
|
|
||||||
|
|
||||||
try {
|
|
||||||
JSON.parse(content)
|
|
||||||
} catch (e) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// console.log('Recreating editor...')
|
// console.log('Recreating editor...')
|
||||||
// editor?.destroy()
|
// editor?.destroy()
|
||||||
// setEditor(newEditor)
|
// setEditor(newEditor)
|
||||||
}, [content])
|
}, [content])
|
||||||
|
|
||||||
|
// Setup: Editor
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
content: formatContent(content),
|
content: formatContent(content),
|
||||||
editable: editable,
|
editable: editable,
|
||||||
|
|
@ -109,6 +97,18 @@ const Editor = ({
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Methods: Convenience
|
||||||
|
function isJSON(content?: string) {
|
||||||
|
if (!content) return false
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSON.parse(content)
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
function formatContent(content?: string) {
|
function formatContent(content?: string) {
|
||||||
if (!content) return ''
|
if (!content) return ''
|
||||||
if (isJSON(content)) return JSON.parse(content)
|
if (isJSON(content)) return JSON.parse(content)
|
||||||
|
|
@ -127,6 +127,7 @@ const Editor = ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods: Actions
|
||||||
const setLink = useCallback(() => {
|
const setLink = useCallback(() => {
|
||||||
const previousUrl = editor?.getAttributes('link').href
|
const previousUrl = editor?.getAttributes('link').href
|
||||||
const url = window.prompt('URL', previousUrl)
|
const url = window.prompt('URL', previousUrl)
|
||||||
|
|
@ -159,6 +160,7 @@ const Editor = ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods: Rendering
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue