{"version":3,"file":"pages/TreeShaking/editor.mjs","sources":["../../../src/pages/TreeShaking/editor.tsx"],"sourcesContent":["import { InfoCircleOutlined, LoadingOutlined } from '@ant-design/icons';\nimport Editor, { OnMount } from '@monaco-editor/react';\nimport { SDK } from '@rsdoctor/types';\nimport { Card, Space, Tooltip, Typography } from 'antd';\nimport type { editor, Range as RangeClass } from 'monaco-editor';\nimport path from 'path-browserify';\nimport { useEffect, useRef, useState } from 'react';\nimport {\n  defineMonacoOptions,\n  getFilePathFormat,\n  getSelectionRange,\n} from 'src/components/base/CodeViewer/utils';\nimport { parseOpenTag } from './open-tag';\nimport { Range } from './range';\nimport { SetEditorStatus } from './types';\nimport { getHoverMessageInModule } from './utils';\n\nexport interface CodeEditorProps {\n  module: SDK.ModuleInstance;\n  moduleGraph: SDK.ModuleGraphInstance;\n  ranges: SDK.SourceRange[];\n  setEditorData: SetEditorStatus;\n  source: SDK.ModuleSource;\n  toLine?: number;\n}\n\nconst defaultEditOption = defineMonacoOptions();\n\nexport function CodeEditor(props: CodeEditorProps) {\n  const { module, moduleGraph, ranges, toLine, setEditorData, source } = props;\n  const editorRef = useRef<editor.IStandaloneCodeEditor>(undefined);\n  const oldRanges = useRef<string[]>([]);\n  const oldHovers = useRef<string[]>([]);\n  const oldToLine = useRef<number>(undefined);\n  const changeModule = useRef(false);\n  const [content, setContent] = useState('');\n  // const [isDynamic, setIsDynamic] = useState(false);\n  // const [exports, setExports] = useState<SDK.ExportData[]>([]);\n\n  useEffect(() => {\n    const { isPreferSource } = module;\n    changeModule.current = true;\n    setContent(\n      source.source || isPreferSource ? source.source : source.transformed,\n    );\n    setTimeout(() => {\n      oldHovers.current =\n        editorRef.current\n          ?.getModel()\n          ?.deltaDecorations(\n            oldHovers.current,\n            getHoverMessageInModule(module, moduleGraph),\n          ) ?? [];\n    }, 200);\n  }, [module, source]);\n\n  useEffect(() => {\n    function setRangeAndLine() {\n      const model = editorRef.current?.getModel();\n\n      if (!model) {\n        return;\n      }\n\n      oldRanges.current = model.deltaDecorations(\n        oldRanges.current,\n        ranges.map((arr) => {\n          return {\n            range: getSelectionRange(\n              arr,\n              Range as unknown as typeof RangeClass,\n            ),\n            options: {\n              stickiness: 1,\n              inlineClassName: 'tree-shaking-statement-side-effect',\n              isWholeLine: false,\n              showIfCollapsed: true,\n            },\n          } as unknown as editor.IModelDecoration;\n        }),\n      );\n\n      if (\n        editorRef.current &&\n        typeof toLine === 'number' &&\n        oldToLine.current !== toLine\n      ) {\n        oldToLine.current = toLine;\n        editorRef.current.revealLine(toLine, 0);\n      }\n    }\n\n    // 模块变更时，高亮和滚动需要等待 300ms，确保在文本变更之后\n    if (changeModule) {\n      setTimeout(setRangeAndLine, 300);\n    } else {\n      setRangeAndLine();\n    }\n\n    changeModule.current = false;\n  }, [ranges, toLine]);\n\n  useEffect(() => {\n    const openEditor = (event: MouseEvent) => {\n      const query = parseOpenTag(event.target as HTMLElement);\n\n      if (query) {\n        const module = moduleGraph.getModuleById(query.module);\n\n        if (module) {\n          setEditorData(module, [query.range], query.range.start.line);\n        }\n      }\n    };\n\n    document.body.addEventListener('click', openEditor);\n\n    return () => {\n      document.body.removeEventListener('click', openEditor);\n    };\n  }, []);\n\n  if (!module) {\n    return <div>请选择要查看的模块</div>;\n  }\n\n  const handleEditorDidMount: OnMount = (editor) => {\n    editorRef.current = editor;\n  };\n\n  return (\n    <Card\n      title={\n        <Tooltip title={module.path}>\n          <Space>\n            <Typography.Text>{path.basename(module.path)}</Typography.Text>\n            <InfoCircleOutlined />\n          </Space>\n        </Tooltip>\n      }\n      className=\"tree-shaking-editor\"\n    >\n      {/* TODO: change to CodeViewer */}\n      <Editor\n        theme=\"vs-dark\"\n        language={getFilePathFormat(module.path)}\n        value={content}\n        loading={<LoadingOutlined style={{ fontSize: 30 }} />}\n        options={defaultEditOption}\n        onMount={handleEditorDidMount}\n      />\n    </Card>\n  );\n}\n"],"names":["defaultEditOption","defineMonacoOptions","CodeEditor","props","module","moduleGraph","ranges","toLine","setEditorData","source","editorRef","useRef","undefined","oldRanges","oldHovers","oldToLine","changeModule","content","setContent","useState","useEffect","isPreferSource","setTimeout","getHoverMessageInModule","setRangeAndLine","model","arr","getSelectionRange","Range","openEditor","event","query","parseOpenTag","document","handleEditorDidMount","editor","Card","Tooltip","Space","Typography","path","InfoCircleOutlined","Editor","getFilePathFormat","LoadingOutlined"],"mappings":";;;;;;;;;;AA0BA,MAAMA,oBAAoBC;AAEnB,SAASC,WAAWC,KAAsB;IAC/C,MAAM,EAAEC,MAAM,EAAEC,WAAW,EAAEC,MAAM,EAAEC,MAAM,EAAEC,aAAa,EAAEC,MAAM,EAAE,GAAGN;IACvE,MAAMO,YAAYC,OAAqCC;IACvD,MAAMC,YAAYF,OAAiB,EAAE;IACrC,MAAMG,YAAYH,OAAiB,EAAE;IACrC,MAAMI,YAAYJ,OAAeC;IACjC,MAAMI,eAAeL,OAAO;IAC5B,MAAM,CAACM,SAASC,WAAW,GAAGC,SAAS;IAIvCC,UAAU;QACR,MAAM,EAAEC,cAAc,EAAE,GAAGjB;QAC3BY,aAAa,OAAO,GAAG;QACvBE,WACET,OAAO,MAAM,IAAIY,iBAAiBZ,OAAO,MAAM,GAAGA,OAAO,WAAW;QAEtEa,WAAW;YACTR,UAAU,OAAO,GACfJ,UAAU,OAAO,EACb,YACA,iBACAI,UAAU,OAAO,EACjBS,wBAAwBnB,QAAQC,iBAC7B,EAAE;QACb,GAAG;IACL,GAAG;QAACD;QAAQK;KAAO;IAEnBW,UAAU;QACR,SAASI;YACP,MAAMC,QAAQf,UAAU,OAAO,EAAE;YAEjC,IAAI,CAACe,OACH;YAGFZ,UAAU,OAAO,GAAGY,MAAM,gBAAgB,CACxCZ,UAAU,OAAO,EACjBP,OAAO,GAAG,CAAC,CAACoB,MACH;oBACL,OAAOC,kBACLD,KACAE;oBAEF,SAAS;wBACP,YAAY;wBACZ,iBAAiB;wBACjB,aAAa;wBACb,iBAAiB;oBACnB;gBACF;YAIJ,IACElB,UAAU,OA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