11import { CopyToast , useCopyToast } from 'components/CopyToast' ;
22import { motion } from 'framer-motion' ;
33import { useLocaleBundle } from 'hooks/useTranslation' ;
4- import { useRouter } from 'next/router' ;
54import { useEffect , useState } from 'react' ;
65import { DEFAULT_SYNTAX } from './defaultSyntax' ;
76import { EditorPanel } from './EditorPanel' ;
87import { PreviewPanel } from './PreviewPanel' ;
9- import { extractContentFromUrl , generateShareUrl } from './utils' ;
108
119const STORAGE_KEY = 'live-editor-syntax' ;
1210
@@ -15,43 +13,28 @@ const TRANSLATIONS = {
1513 title : 'Live Editor' ,
1614 description : '编辑 AntV Infographic Syntax 并查看实时预览' ,
1715 resetButton : '重置为默认' ,
18- shareButton : '分享' ,
1916 imageCopied : '图片已复制到剪贴板' ,
20- linkCopied : '分享链接已复制到剪贴板' ,
21- shareFailed : '生成分享链接失败' ,
2217 } ,
2318 'en-US' : {
2419 title : 'Live Editor' ,
2520 description : 'Edit AntV Infographic Syntax and see real-time preview' ,
2621 resetButton : 'Reset to Default' ,
27- shareButton : 'Share' ,
2822 imageCopied : 'Image copied to clipboard' ,
29- linkCopied : 'Share link copied to clipboard' ,
30- shareFailed : 'Failed to generate share link' ,
3123 } ,
3224} ;
3325
3426export function EditorContent ( ) {
35- const router = useRouter ( ) ;
3627 const [ syntax , setSyntax ] = useState ( DEFAULT_SYNTAX ) ;
3728 const [ error , setError ] = useState < string | null > ( null ) ;
3829 const [ mounted , setMounted ] = useState ( false ) ;
3930 const { message : copyHint , show : showCopyHint } = useCopyToast ( 2500 ) ;
4031 const texts = useLocaleBundle ( TRANSLATIONS ) ;
4132
42- // Load from URL or localStorage on mount
33+ // Load from localStorage on mount
4334 useEffect ( ( ) => {
4435 setMounted ( true ) ;
4536 if ( typeof window === 'undefined' ) return ;
4637
47- // Try URL first
48- const urlContent = extractContentFromUrl ( ) ;
49- if ( urlContent ) {
50- setSyntax ( urlContent ) ;
51- return ;
52- }
53-
54- // Fall back to localStorage
5538 const saved = localStorage . getItem ( STORAGE_KEY ) ;
5639 if ( saved ) {
5740 setSyntax ( saved ) ;
@@ -66,53 +49,11 @@ export function EditorContent() {
6649 localStorage . setItem ( STORAGE_KEY , syntax ) ;
6750 } , [ syntax , mounted ] ) ;
6851
69- // Debounced URL update when content is edited (if URL has content param)
70- useEffect ( ( ) => {
71- if ( ! mounted ) return ;
72- if ( typeof window === 'undefined' ) return ;
73-
74- // Check if current URL has content parameter
75- const hasContentParam = window . location . search . includes ( 'content=' ) ;
76- if ( ! hasContentParam ) return ;
77-
78- // Debounce URL update (same delay as preview: 300ms)
79- const timer = setTimeout ( ( ) => {
80- const shareUrl = generateShareUrl ( syntax ) ;
81- if ( shareUrl ) {
82- window . history . replaceState ( null , '' , shareUrl ) ;
83- }
84- } , 300 ) ;
85-
86- return ( ) => clearTimeout ( timer ) ;
87- } , [ syntax , mounted ] ) ;
88-
8952 const handleReset = ( ) => {
9053 setSyntax ( DEFAULT_SYNTAX ) ;
9154 if ( typeof window !== 'undefined' ) {
9255 localStorage . removeItem ( STORAGE_KEY ) ;
9356 }
94- // Clear URL params
95- router . replace ( '/editor' , undefined , { shallow : true } ) ;
96- } ;
97-
98- const handleShare = async ( ) => {
99- const shareUrl = generateShareUrl ( syntax ) ;
100- if ( ! shareUrl ) {
101- showCopyHint ( texts . shareFailed ) ;
102- return ;
103- }
104-
105- try {
106- await navigator . clipboard . writeText ( shareUrl ) ;
107- showCopyHint ( texts . linkCopied ) ;
108- // Use native history API to update URL without triggering re-render
109- if ( typeof window !== 'undefined' ) {
110- window . history . replaceState ( null , '' , shareUrl ) ;
111- }
112- } catch ( e ) {
113- console . error ( 'Failed to copy share link:' , e ) ;
114- showCopyHint ( texts . shareFailed ) ;
115- }
11657 } ;
11758
11859 return (
@@ -142,7 +83,6 @@ export function EditorContent() {
14283 < EditorPanel
14384 value = { syntax }
14485 onChange = { setSyntax }
145- onShare = { handleShare }
14686 onReset = { handleReset }
14787 />
14888 < PreviewPanel
0 commit comments