|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { FC, useCallback, useEffect, useRef } from "react"; |
| 21 | +import { useTranslation } from "react-i18next"; |
| 22 | + |
| 23 | +import { StacksEditor } from "@stackoverflow/stacks-editor"; |
| 24 | + |
| 25 | +import "@stackoverflow/stacks"; |
| 26 | +import "@stackoverflow/stacks/dist/css/stacks.css"; |
| 27 | + |
| 28 | +import "@stackoverflow/stacks-editor/dist/styles.css"; |
| 29 | + |
| 30 | +export interface EditorProps { |
| 31 | + value: string; |
| 32 | + onChange?: (value: string) => void; |
| 33 | + onFocus?: () => void; |
| 34 | + onBlur?: () => void; |
| 35 | + placeholder?: string; |
| 36 | + autoFocus?: boolean; |
| 37 | + imageUploadHandler?: (file: File | string) => Promise<string>; |
| 38 | + uploadConfig?: { |
| 39 | + maxImageSizeMiB?: number; |
| 40 | + allowedExtensions?: string[]; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +const Component: FC<EditorProps> = ({ |
| 45 | + value, |
| 46 | + onChange, |
| 47 | + onFocus, |
| 48 | + onBlur, |
| 49 | + placeholder = "", |
| 50 | + autoFocus = false, |
| 51 | + imageUploadHandler, |
| 52 | + uploadConfig, |
| 53 | +}) => { |
| 54 | + const { t } = useTranslation("plugin", { |
| 55 | + keyPrefix: "editor_stacks.frontend", |
| 56 | + }); |
| 57 | + const containerRef = useRef<HTMLDivElement>(null); |
| 58 | + const editorInstanceRef = useRef<StacksEditor | null>(null); |
| 59 | + const isInitializedRef = useRef(false); |
| 60 | + |
| 61 | + // Version compatibility temporarily disabled |
| 62 | + |
| 63 | + const syncTheme = useCallback(() => { |
| 64 | + if (!containerRef.current) return; |
| 65 | + |
| 66 | + containerRef.current.parentElement?.classList.remove( |
| 67 | + "theme-light", |
| 68 | + "theme-dark", |
| 69 | + "theme-system", |
| 70 | + ); |
| 71 | + const themeAttr = |
| 72 | + document.documentElement.getAttribute("data-bs-theme") || |
| 73 | + document.body.getAttribute("data-bs-theme"); |
| 74 | + |
| 75 | + if (themeAttr) { |
| 76 | + containerRef.current.parentElement?.classList.add(`theme-system`); |
| 77 | + } |
| 78 | + }, []); |
| 79 | + |
| 80 | + useEffect(() => { |
| 81 | + syncTheme(); |
| 82 | + }, [syncTheme]); |
| 83 | + |
| 84 | + useEffect(() => { |
| 85 | + const observer = new MutationObserver(() => { |
| 86 | + syncTheme(); |
| 87 | + }); |
| 88 | + observer.observe(document.documentElement, { |
| 89 | + attributes: true, |
| 90 | + attributeFilter: ["data-bs-theme", "class"], |
| 91 | + }); |
| 92 | + observer.observe(document.body, { |
| 93 | + attributes: true, |
| 94 | + attributeFilter: ["data-bs-theme", "class"], |
| 95 | + }); |
| 96 | + return () => observer.disconnect(); |
| 97 | + }, [syncTheme]); |
| 98 | + |
| 99 | + useEffect(() => { |
| 100 | + if (!containerRef.current || isInitializedRef.current) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + const container = document.createElement("div"); |
| 105 | + container.className = "stacks-editor-container"; |
| 106 | + container.style.minHeight = "320px"; |
| 107 | + containerRef.current.appendChild(container); |
| 108 | + |
| 109 | + let editorInstance: StacksEditor | null = null; |
| 110 | + |
| 111 | + try { |
| 112 | + editorInstance = new StacksEditor(container, value || "", { |
| 113 | + placeholderText: placeholder || t("placeholder", ""), |
| 114 | + parserFeatures: { |
| 115 | + tables: true, |
| 116 | + html: false, |
| 117 | + }, |
| 118 | + imageUpload: imageUploadHandler |
| 119 | + ? { |
| 120 | + handler: imageUploadHandler, |
| 121 | + sizeLimitMib: uploadConfig?.maxImageSizeMiB, |
| 122 | + acceptedFileTypes: uploadConfig?.allowedExtensions, |
| 123 | + } |
| 124 | + : undefined, |
| 125 | + }); |
| 126 | + |
| 127 | + editorInstanceRef.current = editorInstance; |
| 128 | + isInitializedRef.current = true; |
| 129 | + |
| 130 | + const editor = editorInstance; |
| 131 | + |
| 132 | + const originalDispatch = editor.editorView.props.dispatchTransaction; |
| 133 | + editor.editorView.setProps({ |
| 134 | + dispatchTransaction: (tr) => { |
| 135 | + if (originalDispatch) { |
| 136 | + originalDispatch.call(editor.editorView, tr); |
| 137 | + } else { |
| 138 | + const newState = editor.editorView.state.apply(tr); |
| 139 | + editor.editorView.updateState(newState); |
| 140 | + } |
| 141 | + |
| 142 | + if (tr.docChanged && onChange) { |
| 143 | + const newContent = editor.content; |
| 144 | + onChange(newContent); |
| 145 | + } |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + const editorElement = editor.dom as HTMLElement; |
| 150 | + if (editorElement) { |
| 151 | + const handleFocus = () => onFocus?.(); |
| 152 | + const handleBlur = () => onBlur?.(); |
| 153 | + editorElement.addEventListener("focus", handleFocus, true); |
| 154 | + editorElement.addEventListener("blur", handleBlur, true); |
| 155 | + } |
| 156 | + |
| 157 | + if (autoFocus) { |
| 158 | + setTimeout(() => { |
| 159 | + if (editor) { |
| 160 | + editor.focus(); |
| 161 | + } |
| 162 | + }, 100); |
| 163 | + } |
| 164 | + |
| 165 | + return () => { |
| 166 | + if (editorInstance) { |
| 167 | + try { |
| 168 | + editorInstance.destroy(); |
| 169 | + } catch (e) { |
| 170 | + console.error("Error destroying editor:", e); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + editorInstanceRef.current = null; |
| 175 | + isInitializedRef.current = false; |
| 176 | + |
| 177 | + try { |
| 178 | + container.remove(); |
| 179 | + } catch {} |
| 180 | + }; |
| 181 | + } catch (error) { |
| 182 | + console.error("Failed to initialize Stacks Editor:", error); |
| 183 | + isInitializedRef.current = false; |
| 184 | + } |
| 185 | + }, []); |
| 186 | + |
| 187 | + useEffect(() => { |
| 188 | + const editor = editorInstanceRef.current; |
| 189 | + if (!editor || !isInitializedRef.current) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + try { |
| 194 | + if (editor.content !== value) { |
| 195 | + editor.content = value; |
| 196 | + } |
| 197 | + } catch (error) { |
| 198 | + console.error("Error syncing editor content:", error); |
| 199 | + } |
| 200 | + }, [value]); |
| 201 | + |
| 202 | + return ( |
| 203 | + <div className="editor-stacks-wrapper editor-stacks-scope"> |
| 204 | + <div ref={containerRef} style={{ minHeight: 320 }} /> |
| 205 | + </div> |
| 206 | + ); |
| 207 | +}; |
| 208 | + |
| 209 | +export default Component; |
0 commit comments