質問するログイン新規登録

質問編集履歴

1

コードを載せました。

2023/01/01 04:14

投稿

Boson328
Boson328

スコア0

title CHANGED
File without changes
body CHANGED
@@ -20,4 +20,132 @@
20
20
  ### 補足情報(FW/ツールのバージョンなど)
21
21
 
22
22
  Editorjsのバージョンは2.26.4です。
23
- react-editor-jsを使っています。
23
+ react-editor-jsを使っています。
24
+
25
+ 今のコード
26
+ ```Javascript
27
+ import { createReactEditorJS } from "react-editor-js";
28
+ import React, { useEffect, useReducer, useState, useRef } from "react";
29
+ import DragDrop from "editorjs-drag-drop";
30
+ import { useParams } from "react-router";
31
+ import Marker from "@editorjs/marker";
32
+ import Header from "@editorjs/header";
33
+ import CodeTool from "@editorjs/code";
34
+ import Quote from "@editorjs/quote";
35
+ import Embed from "@editorjs/embed";
36
+ import List from "@editorjs/list";
37
+ import InlineCode from "@editorjs/inline-code";
38
+ import Head from "../components/Head";
39
+ import { db } from "../firebase";
40
+
41
+ function NoteApp() {
42
+ const ReactEditorJS = createReactEditorJS();
43
+ const instanceRef = useRef(null);
44
+ const params = useParams();
45
+ console.log(instanceRef);
46
+ const [note, setNote] = useState();
47
+ const [pageTitle, setPagetitle] = useState();
48
+
49
+ const editorCore = React.useRef(null);
50
+ const handleInitialize = React.useCallback((instance) => {
51
+ editorCore.current = instance;
52
+ }, []);
53
+
54
+ const handleReady = () => {
55
+ const editor = editorCore.current._editorJS;
56
+ new DragDrop(editor);
57
+ let codexeditor = document.getElementsByClassName("codex-editor");
58
+ for (let i = 0; i < codexeditor.length; i++) {
59
+ codexeditor[i].classList.remove("codex-editor--narrow");
60
+ }
61
+ };
62
+
63
+ const handleSave = React.useCallback(async () => {
64
+ let savedData = await editorCore.current.save();
65
+ console.log(savedData);
66
+ // ここでFirestoreにデータを格納
67
+ db.collection("workspaces")
68
+ .doc(params.workspace)
69
+ .collection("projects")
70
+ .doc(params.project)
71
+ .collection("note")
72
+ .doc(document.getElementById("textedit").className)
73
+ .update({ note: savedData });
74
+ }, []);
75
+
76
+ useEffect(() => {
77
+ // ここでFirestoreからデータを取得
78
+ db.collection("workspaces")
79
+ .doc(params.workspace)
80
+ .collection("projects")
81
+ .doc(params.project)
82
+ .collection("note")
83
+ .doc(params.page)
84
+ .get()
85
+ .then((snapshot) => {
86
+ console.log(snapshot.data());
87
+ setNote([]);
88
+ setNote(snapshot.data().note);
89
+ });
90
+ db.collection("workspaces")
91
+ .doc(params.workspace)
92
+ .collection("projects")
93
+ .doc(params.project)
94
+ .collection("note")
95
+ .doc(params.page)
96
+ .onSnapshot((snapshot) => {
97
+ setPagetitle(snapshot.data().title);
98
+ });
99
+ }, [params]);
100
+
101
+ useEffect(async () => {
102
+ var noteDoc = await db
103
+ .collection("workspaces")
104
+ .doc(params.workspace)
105
+ .collection("projects")
106
+ .doc(params.project)
107
+ .collection("note")
108
+ .doc(params.page)
109
+ .get();
110
+ if (!noteDoc.exists) {
111
+ window.location.href = "/app/" + params.workspace + "/" + params.project;
112
+ }
113
+ }, []);
114
+
115
+ return (
116
+ <div id="app" className={localStorage.getItem("handleBanner")}>
117
+ {console.log(note)}
118
+ <div id="textedit" className={params.page}>
119
+ {note == undefined ? (
120
+ <Head title="Newtron" />
121
+ ) : (
122
+ <>
123
+ <ReactEditorJS
124
+ instanceRef={(instance) => (instanceRef.current = instance)}
125
+ onReady={handleReady}
126
+ onInitialize={handleInitialize}
127
+ placeholder="何か入力"
128
+ onChange={handleSave}
129
+ value={note}
130
+ defaultValue={note}
131
+ autofocus="true"
132
+ defaultBlock="paragraph"
133
+ tools={{
134
+ marker: Marker,
135
+ heading: Header,
136
+ codetool: CodeTool,
137
+ quote: Quote,
138
+ embed: Embed,
139
+ inlinecode: InlineCode,
140
+ list: List,
141
+ }}
142
+ />
143
+ </>
144
+ )}
145
+ </div>
146
+ </div>
147
+ );
148
+ }
149
+
150
+ export default NoteApp;
151
+ ```