import ReactDOM, { unstable_batchedUpdates } from "react-dom"; import { useCallback, useState, useEffect, useRef } from "react"; import Editor from "../src/multi"; import { themes } from "@utils/initEditor"; import { THEMES, ASSETSPATH } from "@utils/consts"; import { copyDataToClipBoard } from "@utils"; interface filelist { [key: string]: string; } declare global { interface Window { eslint: any; linter: any; } } const filesName = [ "/index.html", "/app/index.jsx", "/app/index.css", "/app/button.jsx", "/src/index.jsx", "/src/components/index.jsx", // '/app.js', // '/cc.js', "/app.ts", "/cc.ts", // '/test.css', // '/src/index.jsx', "/style.md", // '/styles/index.less', // '/src/components/title/index.js', // '/src/components/title/index.less', ]; const code = ` import React from "react"; import { definePage } from "@music/tango-boot"; import { Page, Section, Text, Button, DateRangePicker, Layout, } from "@music/tango-cms"; import { message } from "@music/tango-cms"; class App extends React.Component { componentDidMount() { message.info("page mounted!"); } render() { const { routeData } = this.props; return (
hello 123 baidu
); } } export default definePage(App); `; const App = () => { const [files, setFiles] = useState({}); const editorRef = useRef(null); const [colors, setColors] = useState<{ [key: string]: string; }>({}); useEffect(() => { // 获取多文件 const promises = filesName.map( async (v) => await (await fetch(`${ASSETSPATH}editorfiles${v}`)).text() ); Promise.all(promises).then((filesContent) => { const res: filelist = {}; filesContent.forEach((content, index) => { res[filesName[index]] = content; }); res["/test.js"] = code; unstable_batchedUpdates(() => { setFiles(res); // setPath(filesName[0]); // setValue(filesContent[0]); }); }); setTimeout(() => { console.log(themes); setColors(themes["GithubLightDefault"].colors); }, 5000); }, []); // 设置当前文件路径和value const handlePathChange = useCallback((key: string) => { console.log(key); }, []); // // 同步ide内容修改 // const handleChange = useCallback((v: string) => { // console.log(v); // }, []); // const handleFileChange = (key: string, value: string) => { // console.log(key, value); // } const handleThemeChange = (e: any) => { editorRef.current.setTheme(e.target.value); }; // const sandboxRef = useRef(null); // const sendMessage = () => { // // @ts-ignore // sandboxRef.current.contentWindow.postMessage({ // type: 'SAVE_FILES', // files, // }, 'http://localhost:8081'); // }; // useEffect(() => { // window.addEventListener('message', res => console.log(res)); // }, []); const [activePath, setActivePath] = useState("/app.ts"); useEffect(() => { console.log(files); // if (editorRef.current) { // editorRef.current.refresh(files, activePath, { // start: { // line: 1, // column: 1, // }, // end: { // line: 5, // column: 1, // } // }); // } }); const addFile = useCallback(() => { setActivePath("/src/pages/new-page1.js"); setFiles((pre) => ({ ...pre, "/src/pages/new-page1.js": `export default const foo = 'hello';`, })); }, []); return (
addFile
console.log(editorRef.current)}>ref api
console.log( editorRef.current.refresh(files, "/style.md", { start: { line: 29, column: 1, }, end: { line: 40, column: 1, }, }) ) } > refresh
{ console.log(editorRef.current.getAllValue()); }} > real getAllvalue
setColors(themes["OneDarkPro"].colors)}> refresh theme color
{Object.keys(files).length > 0 && (
{ setActivePath(path); }} // onFileSave={(key: string, value: string) => { // console.log(editorRef.current.getAllValue()); // }} onFileChange={(...args) => console.log(args)} onRenameFile={(...args) => { // setFiles((pre) => { // const res = {...pre}; // res[args[1]] = res[args[0]]; // delete res[args[0]]; // return res; // }); // setActivePath(args[1]); console.log(args); }} ref={editorRef} defaultPath={activePath} defaultFiles={files} options={{ fontSize: 14, automaticLayout: true, }} />
)} {/*