Skip to content

Commit

Permalink
chore: add reactive editable editor
Browse files Browse the repository at this point in the history
  • Loading branch information
hunghg255 committed Nov 3, 2024
1 parent 6cc6c26 commit 56090e9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/components/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { removeCSS, updateCSS } from '@/utils/dynamicCSS'

import '../styles/index.scss'
import CharactorCount from '@/components/CharactorCount'
import { editableEditorActions } from '@/store/editableEditor'

/**
* Interface for RichTextEditor component props
Expand Down Expand Up @@ -110,6 +111,7 @@ function RichTextEditor(props: RichTextEditorProps, ref: React.ForwardedRef<{ ed

useEffect(() => {
editor?.setEditable(!props?.disabled)
editableEditorActions.setDisable(!props?.disabled)
}, [editor, props?.disabled])

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import styles from './index.module.scss'
import { extractFileExtension, extractFilename, normalizeFileSize } from '@/utils/file'
import { ActionButton } from '@/components'
import { useLocale } from '@/locales'
import { useEditableEditor } from '@/store/editableEditor'

export function NodeViewAttachment({ editor, node, updateAttributes, deleteNode, extension }: any) {
const $upload: any = useRef<HTMLInputElement>()
const isEditable = editor.isEditable

const isEditable = useEditableEditor()

const { hasTrigger, fileName, fileSize, fileExt, fileType, url, error } = node.attrs
const [loading, setLoading] = useState(false)
const { t } = useLocale()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useCopy from '@/hooks/useCopy'
import { deleteNode } from '@/utils/delete-node'
import { DEFAULT_LANGUAGE_CODE_BLOCK } from '@/constants'
import { IconComponent } from '@/components'
import { useEditableEditor } from '@/store/editableEditor'

export function NodeViewCodeBlock({ editor, node: { attrs }, updateAttributes, extension }: any) {
const { isCopied, copyToClipboard } = useCopy()
Expand All @@ -21,7 +22,8 @@ export function NodeViewCodeBlock({ editor, node: { attrs }, updateAttributes, e
return extension.options.languages?.length ? extension.options.languages : DEFAULT_LANGUAGE_CODE_BLOCK
}, [extension.options.languages])

const isEditable = editor.isEditable
const isEditable = useEditableEditor()

const isPrint = editor?.options?.editorProps?.print
const { language: defaultLanguage } = attrs
const $container: any = useRef<HTMLPreElement>()
Expand All @@ -31,7 +33,11 @@ export function NodeViewCodeBlock({ editor, node: { attrs }, updateAttributes, e
return (
<NodeViewWrapper className={clsx(styles.wrap, !isPrint && styles.maxHeight, 'render-wrapper')}>

<div className={styles.blockInfo}>
<div className={clsx(styles.blockInfo, {
[styles.blockInfoEditable]: !isEditable,
})}

>
<span
onClick={() => copyToClipboard($container.current.innerText)}
className={clsx(styles.btnCopy, isCopied && styles.copied)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
gap: 8px;
}

.blockInfoEditable {
display: none;
}

.selectLang {
cursor: pointer;
transition: all .3s;
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/Iframe/components/IframeNodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import styles from './index.module.scss'
// import { getEditorContainerDOMSize } from '@/utils'
import { Button, Input } from '@/components/ui'
import { Iframe } from '@/extensions/Iframe/Iframe'
import { useEditableEditor } from '@/store/editableEditor'

function IframeNodeView({ editor, node, updateAttributes }: any) {
const isEditable = editor.isEditable
const isEditable = useEditableEditor()

const { url, width, height } = node.attrs
// const { width: maxWidth } = getEditorContainerDOMSize(editor)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react/no-duplicate-key */
/* eslint-disable ts/no-unused-expressions */
import { useCallback, useEffect, useState } from 'react'

Expand All @@ -8,6 +7,7 @@ import cls from 'clsx'

import styles from './index.module.scss'
import { useLocale } from '@/locales'
import { useEditableEditor } from '@/store/editableEditor'

function arrToTree(tocs: any) {
const result = [] as any
Expand All @@ -29,7 +29,7 @@ function arrToTree(tocs: any) {
}

export function NodeViewTableOfContent({ editor }: any) {
const isEditable = editor.isEditable
const isEditable = useEditableEditor()
const [items, setItems] = useState([])
const { t } = useLocale()

Expand Down
16 changes: 16 additions & 0 deletions src/store/editableEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { proxy, useSnapshot } from 'valtio'

const editableEditorProxy = proxy({
disable: false,
})

export function useEditableEditor() {
const editableEditor = useSnapshot(editableEditorProxy)
return editableEditor.disable
}

export const editableEditorActions = {
setDisable: (disable: boolean) => {
editableEditorProxy.disable = disable
},
}

0 comments on commit 56090e9

Please sign in to comment.