From a802393e0add95fc8ae33beb8e20f5dc36b149e9 Mon Sep 17 00:00:00 2001 From: sbsoso0411 Date: Tue, 11 Jul 2023 12:11:11 +0200 Subject: [PATCH] edit the code freely --- src/components/main/codeView/CodeView.tsx | 64 ++++++++++++++++++----- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/src/components/main/codeView/CodeView.tsx b/src/components/main/codeView/CodeView.tsx index cc07563c..bd3cfc22 100644 --- a/src/components/main/codeView/CodeView.tsx +++ b/src/components/main/codeView/CodeView.tsx @@ -20,6 +20,7 @@ import { RootNodeUid, } from '@_constants/main'; import { + checkValidHtml, getSubNodeUidsByBfs, TFileNodeData, THtmlNodeData, @@ -111,6 +112,20 @@ export default function CodeView(props: CodeViewProps) { const decorationCollectionRef = useRef() const codeChangeDecorationRef = useRef>(new Map()) const validNodeTreeRef = useRef({}) + const noNeedClosingTag = ['area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'link', + 'meta', + 'param', + 'source', + 'track', + 'wbr'] const handleEditorDidMount = useCallback((editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => { monacoRef.current = editor editor.onDidChangeCursorPosition((event) => { @@ -337,6 +352,8 @@ export default function CodeView(props: CodeViewProps) { const currentCode = codeContent.current const currentCodeArr = currentCode.split(getLineBreaker(osType)) const codeChanges: TCodeChange[] = [] + let hasMismatchedTags = false + for (const codeChange of codeChangeDecorationRef.current.entries()) { let uid = codeChange[0] // check if editing tags are or
@@ -380,23 +397,45 @@ export default function CodeView(props: CodeViewProps) {
           }
         }
         if (htmlSkeletonStructureCount >= 2) {
-          codeChanges.push({ uid , content })
+
+          hasMismatchedTags = checkValidHtml(codeContent.current)
+          
+          if (hasMismatchedTags === false) codeChanges.push({ uid , content })
         }
         else{
           console.log("Can't remove this element because it's an unique element of this page")
         }
       }
-      setCodeChanges(codeChanges)
-      
-      // update
-      dispatch(setCurrentFileContent(codeContent.current))
-      addRunningActions(['processor-updateOpt'])
-      setUpdateOpt({ parse: true, from: 'code' })
-  
-      codeChangeDecorationRef.current.clear()
-      reduxTimeout.current = null
-      setFocusedNode(undefined)
-      currentPosition.current && monacoRef.current?.setPosition(currentPosition.current)
+
+      if (hasMismatchedTags === false) {
+        setCodeChanges(codeChanges)
+        
+        // update
+        dispatch(setCurrentFileContent(codeContent.current))
+        addRunningActions(['processor-updateOpt'])
+        setUpdateOpt({ parse: true, from: 'code' })
+    
+        codeChangeDecorationRef.current.clear()
+        reduxTimeout.current = null
+        setFocusedNode(undefined)
+      }
+      else{
+        // update
+        dispatch(setCurrentFileContent(codeContent.current))
+        setFSPending(false)
+        const _file = JSON.parse(JSON.stringify(ffTree[file.uid])) as TNode
+        addRunningActions(['processor-updateOpt'])
+        const fileData = _file.data as TFileNodeData
+        (ffTree[file.uid].data as TFileNodeData).content = codeContent.current;
+        (ffTree[file.uid].data as TFileNodeData).contentInApp = codeContent.current;
+        (ffTree[file.uid].data as TFileNodeData).changed = codeContent.current !== fileData.orgContent;
+        setFFTree(ffTree)
+        console.log('non-html')
+        dispatch(setCurrentFileContent(codeContent.current))
+        codeChangeDecorationRef.current.clear()
+        setCodeEditing(false)
+        setFSPending(false)
+      }
     }
     else {
       // non-parse file save
@@ -407,6 +446,7 @@ export default function CodeView(props: CodeViewProps) {
       (ffTree[file.uid].data as TFileNodeData).contentInApp = codeContent.current;
       (ffTree[file.uid].data as TFileNodeData).changed = codeContent.current !== fileData.orgContent;
       setFFTree(ffTree)
+      console.log('non-html')
       dispatch(setCurrentFileContent(codeContent.current))
       codeChangeDecorationRef.current.clear()
       setCodeEditing(false)