File size: 4,967 Bytes
561e6f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { NodeViewWrapper } from "@tiptap/react";
import { useCallback, useRef, useState } from "react";
import type { NodeViewProps } from "@tiptap/react";
import { uploadImage } from "./upload";

export function ImageUploadView({ editor, getPos }: NodeViewProps) {
  const [dragging, setDragging] = useState(false);
  const [uploading, setUploading] = useState(false);
  const [urlMode, setUrlMode] = useState(false);
  const [urlValue, setUrlValue] = useState("");
  const fileRef = useRef<HTMLInputElement>(null);

  const replaceWithImage = useCallback(
    (src: string) => {
      const pos = getPos();
      if (pos === undefined) return;
      editor
        .chain()
        .focus()
        .deleteRange({ from: pos, to: pos + 1 })
        .insertContentAt(pos, {
          type: "image",
          attrs: { src },
        })
        .run();
    },
    [editor, getPos],
  );

  const handleFiles = useCallback(
    (files: FileList | File[]) => {
      const file = Array.from(files).find((f) => f.type.startsWith("image/"));
      if (!file) return;
      setUploading(true);
      uploadImage(file)
        .then((url) => replaceWithImage(url))
        .catch((err) => {
          console.error("[image-upload]", err);
          setUploading(false);
        });
    },
    [replaceWithImage],
  );

  const onDrop = useCallback(
    (e: React.DragEvent) => {
      e.preventDefault();
      setDragging(false);
      if (e.dataTransfer.files.length) handleFiles(e.dataTransfer.files);
    },
    [handleFiles],
  );

  const onDragOver = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragging(true);
  }, []);

  const onDragLeave = useCallback(() => setDragging(false), []);

  const submitUrl = useCallback(() => {
    const src = urlValue.trim();
    if (src) replaceWithImage(src);
  }, [urlValue, replaceWithImage]);

  return (
    <NodeViewWrapper>
      <div
        className={`image-upload-card${dragging ? " dragging" : ""}`}
        onDrop={onDrop}
        onDragOver={onDragOver}
        onDragLeave={onDragLeave}
        contentEditable={false}
      >
        <input
          ref={fileRef}
          type="file"
          accept="image/*"
          style={{ display: "none" }}
          onChange={(e) => {
            if (e.target.files?.length) handleFiles(e.target.files);
          }}
        />

        {uploading ? (
          <>
            <div className="image-upload-card-icon" style={{ opacity: 0.5 }}>
              <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
                <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
                <circle cx="8.5" cy="8.5" r="1.5" />
                <polyline points="21 15 16 10 5 21" />
              </svg>
            </div>
            <p className="image-upload-card-hint">Uploading...</p>
          </>
        ) : !urlMode ? (
          <>
            <div className="image-upload-card-icon">
              <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
                <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
                <circle cx="8.5" cy="8.5" r="1.5" />
                <polyline points="21 15 16 10 5 21" />
              </svg>
            </div>
            <div className="image-upload-card-actions">
              <button
                className="image-upload-btn primary"
                onClick={() => fileRef.current?.click()}
              >
                Upload image
              </button>
              <button
                className="image-upload-btn secondary"
                onClick={() => setUrlMode(true)}
              >
                Embed link
              </button>
            </div>
            <p className="image-upload-card-hint">
              Drag & drop an image, or click to upload
            </p>
          </>
        ) : (
          <div className="image-upload-card-url">
            <input
              type="text"
              className="image-upload-url-input"
              placeholder="Paste image URL..."
              value={urlValue}
              onChange={(e) => setUrlValue(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter") submitUrl();
                if (e.key === "Escape") setUrlMode(false);
              }}
              autoFocus
            />
            <div className="image-upload-card-actions">
              <button className="image-upload-btn primary" onClick={submitUrl}>
                Embed
              </button>
              <button
                className="image-upload-btn secondary"
                onClick={() => setUrlMode(false)}
              >
                Cancel
              </button>
            </div>
          </div>
        )}
      </div>
    </NodeViewWrapper>
  );
}