-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
128 lines (110 loc) · 3.61 KB
/
App.jsx
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
import { useEffect, useState, useRef, useContext } from "react";
import { SyncContext } from "./Context/Sync";
import { debounce, getCurrentTab, getCurrentTabStatus, emitter } from "./utils";
import initQuillEditor from "./initQuill";
import "./App.scss";
function App({ type }) {
const { tabInfo, activeContent, setActiveContent } = useContext(SyncContext);
let quillRef = useRef(null);
let quill = null;
useEffect(() => {
quillRef.current = initQuillEditor(
document.getElementById("editor-container")
);
quill = quillRef.current;
quill.on("text-change", onChange);
if (type === "new-editor" && false) {
quill.setContents([]);
} else if (activeContent?.content) {
let pushTitle = [];
// If there is no title exist append title as heading or if it is active content
if (!activeContent?.isChanged || activeContent?.content?.length === 0) {
pushTitle = [
{
insert: activeContent?.title ?? "",
},
{
insert: "\n",
attributes: {
header: 2,
},
},
];
}
quill.setContents([]);
quill.updateContents([...pushTitle, ...activeContent?.content]);
}
return () => {
if (quill) {
quill.off("text-change", onChange);
}
};
}, [type, activeContent]);
const copyContentFromEditor = ({ onSuccess }) => {
if (quillRef.current) {
const type = "text/html";
const blob = new Blob([quillRef.current.root.innerHTML], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(
function () {
onSuccess();
},
/* failure */
function (error) {
console.error("Unable to write to clipboard. :-(");
}
);
}
};
useEffect(() => {
emitter.on("copy-current", copyContentFromEditor);
return () => {
emitter.off("copy-current", copyContentFromEditor);
};
}, [copyContentFromEditor, quill]);
const onChange = debounce(function (deltaChange, oldDelta, source) {
if (source === "user") {
chrome.storage.local.get("store", async (items) => {
if (chrome.runtime.lastError) {
return chrome.runtime.lastError;
}
const { store } = items;
const { pathname, hostname, title } = await getCurrentTabStatus();
/**
* Get current page info.
* There can be a case user can update current page text as well as any other page content
* on any other page.
* This check will ensure we update data to correct state
*/
const pageHostName = activeContent?.hostname ?? hostname;
const pagePathName = activeContent?.pathname ?? pathname;
const allUrlSpecificData = store?.[pageHostName];
const specificPageData = allUrlSpecificData?.[pagePathName];
const pageTitle = allUrlSpecificData?.[pagePathName]?.title;
const newStore = {
...store,
[pageHostName]: {
...allUrlSpecificData,
[pagePathName]: {
...specificPageData,
isChanged: true,
hostname: pageHostName,
pathname: pagePathName,
...(!pageTitle && { title }),
content: [...quillRef.current.editor.getDelta().ops],
},
},
};
chrome.storage.local.set({ store: newStore }, function (items) {
console.log("Success");
});
});
}
}, 200);
return (
<div className="App">
<div id="editor-container"></div>
</div>
);
}
export default App;