|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { EditorComponent, ImageTool, Tool, editor } from "edkit"; |
| 4 | +import { computed, observable } from "mobx"; |
| 5 | +import { observer } from "mobx-react"; |
| 6 | +import { FormComponent, FormComponentProps } from "mobx-react-helper"; |
| 7 | +import { createRef } from "react"; |
| 8 | +import { Constructor } from "web-utility"; |
| 9 | + |
| 10 | +import { AudioTool, DefaultTools, VideoTool } from "./tools"; |
| 11 | +import { cn } from "@/lib/utils"; |
| 12 | + |
| 13 | +export interface EditorProps extends FormComponentProps { |
| 14 | + tools?: Constructor<Tool>[]; |
| 15 | + className?: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface Editor extends EditorComponent {} |
| 19 | + |
| 20 | +@observer |
| 21 | +@editor |
| 22 | +export class Editor |
| 23 | + extends FormComponent<EditorProps> |
| 24 | + implements EditorComponent |
| 25 | +{ |
| 26 | + static displayName = "Editor"; |
| 27 | + |
| 28 | + box = createRef<HTMLDivElement>(); |
| 29 | + |
| 30 | + @observable |
| 31 | + accessor cursorPoint = ""; |
| 32 | + |
| 33 | + @computed |
| 34 | + get toolList(): Tool[] { |
| 35 | + return (this.observedProps.tools || DefaultTools).map( |
| 36 | + (ToolButton) => new ToolButton() |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + @computed |
| 41 | + get imageTool() { |
| 42 | + return this.toolList.find( |
| 43 | + (tool) => tool instanceof ImageTool |
| 44 | + ) as ImageTool; |
| 45 | + } |
| 46 | + |
| 47 | + @computed |
| 48 | + get audioTool() { |
| 49 | + return this.toolList.find( |
| 50 | + (tool) => tool instanceof AudioTool |
| 51 | + ) as AudioTool; |
| 52 | + } |
| 53 | + |
| 54 | + @computed |
| 55 | + get videoTool() { |
| 56 | + return this.toolList.find( |
| 57 | + (tool) => tool instanceof VideoTool |
| 58 | + ) as VideoTool; |
| 59 | + } |
| 60 | + |
| 61 | + componentDidMount() { |
| 62 | + super.componentDidMount(); |
| 63 | + |
| 64 | + const { defaultValue } = this.props; |
| 65 | + |
| 66 | + if (defaultValue != null) this.box.current!.innerHTML = defaultValue; |
| 67 | + |
| 68 | + document.addEventListener("selectionchange", this.updateTools); |
| 69 | + } |
| 70 | + |
| 71 | + componentWillUnmount() { |
| 72 | + super.componentWillUnmount(); |
| 73 | + |
| 74 | + document.removeEventListener("selectionchange", this.updateTools); |
| 75 | + } |
| 76 | + |
| 77 | + updateTools = () => { |
| 78 | + if (this.box.current !== document.activeElement) return; |
| 79 | + |
| 80 | + const selection = getSelection(); |
| 81 | + if (!selection || selection.rangeCount === 0) return; |
| 82 | + |
| 83 | + const { endContainer } = selection.getRangeAt(0) || {}; |
| 84 | + const element = |
| 85 | + endContainer instanceof Element |
| 86 | + ? endContainer |
| 87 | + : endContainer?.parentElement; |
| 88 | + |
| 89 | + const rect = element?.getBoundingClientRect(); |
| 90 | + if (rect) { |
| 91 | + this.cursorPoint = [rect.x, rect.y].join(""); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + updateValue = (markup: string) => (this.innerValue = markup.trim()); |
| 96 | + |
| 97 | + render() { |
| 98 | + // Don't remove unused variable `cursorPoint`, which is used for triggering updates |
| 99 | + const { cursorPoint, toolList, innerValue } = this; |
| 100 | + const { name, className } = this.props; |
| 101 | + |
| 102 | + return ( |
| 103 | + <> |
| 104 | + <header className="flex flex-wrap gap-1 mb-2"> |
| 105 | + {toolList.map((tool) => tool.render(this.box))} |
| 106 | + </header> |
| 107 | + <div |
| 108 | + ref={this.box} |
| 109 | + className={cn( |
| 110 | + "min-h-[200px] w-full rounded-md border border-input bg-background px-3 py-2", |
| 111 | + "text-base shadow-xs outline-none", |
| 112 | + "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", |
| 113 | + "disabled:cursor-not-allowed disabled:opacity-50", |
| 114 | + className |
| 115 | + )} |
| 116 | + contentEditable |
| 117 | + onInput={({ currentTarget: { innerHTML } }) => |
| 118 | + this.updateValue(innerHTML) |
| 119 | + } |
| 120 | + onPaste={this.handlePasteDrop} |
| 121 | + onDrop={this.handlePasteDrop} |
| 122 | + /> |
| 123 | + <input type="hidden" name={name} value={innerValue} /> |
| 124 | + </> |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments