-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathAnnotationEdit.tsx
300 lines (261 loc) · 14.9 KB
/
AnnotationEdit.tsx
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==
import * as stylesButtons from "readium-desktop/renderer/assets/styles/components/buttons.scss";
import * as stylesAnnotations from "readium-desktop/renderer/assets/styles/components/annotations.scss";
import * as React from "react";
import { IAnnotationState, IColor, TDrawType } from "readium-desktop/common/redux/states/renderer/annotation";
import { useTranslator } from "readium-desktop/renderer/common/hooks/useTranslator";
import { useSelector } from "readium-desktop/renderer/common/hooks/useSelector";
import { IReaderRootState } from "readium-desktop/common/redux/states/renderer/readerRootState";
import * as Popover from "@radix-ui/react-popover";
// import * as PenIcon from "readium-desktop/renderer/assets/icons/pen-icon.svg";
import SVG from "readium-desktop/renderer/common/components/SVG";
import * as CheckIcon from "readium-desktop/renderer/assets/icons/doubleCheck-icon.svg";
import * as SaveIcon from "readium-desktop/renderer/assets/icons/floppydisk-icon.svg";
import * as HighLightIcon from "readium-desktop/renderer/assets/icons/highlight-icon.svg";
import * as UnderLineIcon from "readium-desktop/renderer/assets/icons/underline-icon.svg";
import * as TextStrikeThroughtIcon from "readium-desktop/renderer/assets/icons/TextStrikethrough-icon.svg";
import * as TextOutlineIcon from "readium-desktop/renderer/assets/icons/TextOutline-icon.svg";
import * as TagIcon from "readium-desktop/renderer/assets/icons/tag-icon.svg";
import { useDispatch } from "readium-desktop/renderer/common/hooks/useDispatch";
import { readerLocalActionSetConfig } from "../redux/actions";
import classNames from "classnames";
import { TextArea } from "react-aria-components";
import { ComboBox, ComboBoxItem } from "readium-desktop/renderer/common/components/ComboBox";
import { ObjectKeys } from "readium-desktop/utils/object-keys-values";
import { hexToRgb, rgbToHex } from "readium-desktop/common/rgb";
import { TTranslatorKeyParameter } from "readium-desktop/typings/en.translation-keys";
// import { readiumCSSDefaults } from "@r2-navigator-js/electron/common/readium-css-settings";
interface IProps {
save: (color: IColor, comment: string, drawType: TDrawType, tags: string[]) => void;
cancel: () => void;
uuid?: string;
dockedMode: boolean;
btext?: string;
}
// __("reader.annotations.colors.red")
// __("reader.annotations.colors.orange")
// __("reader.annotations.colors.yellow")
// __("reader.annotations.colors.green")
// __("reader.annotations.colors.bluegreen")
// __("reader.annotations.colors.lightblue")
// __("reader.annotations.colors.cyan")
// __("reader.annotations.colors.purple")
export const annotationsColorsLight: Record<string, TTranslatorKeyParameter> = {
"#EB9694": "reader.annotations.colors.red",
"#FAD0C3": "reader.annotations.colors.orange",
"#FEF3BD": "reader.annotations.colors.yellow",
"#C1EAC5": "reader.annotations.colors.green",
"#BEDADC": "reader.annotations.colors.bluegreen",
"#C4DEF6": "reader.annotations.colors.lightblue",
"#BED3F3": "reader.annotations.colors.cyan",
"#D4C4FB": "reader.annotations.colors.purple",
};
const drawType: TDrawType[] = [
"solid_background",
"underline",
"strikethrough",
"outline",
];
export const AnnotationEdit: React.FC<IProps> = (props) => {
const { save, cancel, uuid, dockedMode} = props;
const displayFromReaderMenu = !!uuid;
const [__] = useTranslator();
const { annotation_defaultColor, annotation_defaultDrawType } = useSelector((state: IReaderRootState) => state.reader.config);
const { locatorExtended } = useSelector((state: IReaderRootState) => state.annotation);
const annotationReaderState = useSelector((state: IReaderRootState) => state.reader.annotation);
let annotationState: IAnnotationState = { uuid: "", color: annotation_defaultColor, comment: "", drawType: annotation_defaultDrawType, locatorExtended, created: 0 };
if (uuid) {
const tpl = annotationReaderState.find(([, annotationState]) => annotationState.uuid === uuid);
if (tpl) {
const [, iannotationState] = tpl;
if (iannotationState) {
annotationState = iannotationState;
}
}
}
const colorStr = rgbToHex(annotationState.color);
const [colorSelected, setColor] = React.useState(colorStr);
const dispatch = useDispatch();
const colorObj = hexToRgb(colorSelected) || annotationState.color;
const previousColorSelected = React.useRef<IColor>(colorObj);
const textAreaRef = React.useRef<HTMLTextAreaElement>();
const [drawTypeSelected, setDrawType] = React.useState(annotationState.drawType);
const previousDrawTypeSelected = React.useRef<TDrawType>(drawTypeSelected);
const [tag, setTag] = React.useState<string>((annotationState.tags || [])[0] || "");
const tagsIndexList = useSelector((state: IReaderRootState) => state.annotationTagsIndex);
const selectTagOption = ObjectKeys(tagsIndexList).map((v, i) => ({id: i, name: v}));
const annotationMaxLength = 1500;
const [annotationLength, setAnnotationLength] = React.useState(annotationState.comment.length);
const drawIcon = [
HighLightIcon,
UnderLineIcon,
TextStrikeThroughtIcon,
TextOutlineIcon,
];
// switch (drawType) {
// case "solid_background":
// drawIcon = HighLightIcon;
// break;
// case "underline":
// drawIcon = UnderLineIcon;
// break;
// case "strikethrough":
// drawIcon = TextStrikeThroughtIcon;
// break;
// case "outline":
// drawIcon = TextOutlineIcon;
// break;
// }
const saveConfig = () => {
let flag = false;
if (previousColorSelected.current.red !== colorObj.red || previousColorSelected.current.blue !== colorObj.blue || previousColorSelected.current.green !== colorObj.green) {
flag = true;
}
if (previousDrawTypeSelected.current !== drawTypeSelected) {
flag = true;
}
if (flag) {
const annotation_defaultColor = { ...colorObj };
const annotation_defaultDrawType = drawTypeSelected;
dispatch(readerLocalActionSetConfig.build({ annotation_defaultColor, annotation_defaultDrawType }));
}
previousColorSelected.current = { ...colorObj };
previousDrawTypeSelected.current = drawTypeSelected;
};
React.useEffect(() => {
if (textAreaRef.current) {
textAreaRef.current.style.height = "auto";
textAreaRef.current.style.height = `${textAreaRef.current.scrollHeight + 3}px`;
textAreaRef.current.focus();
}
}, []); // empty => runs once on mount (undefined => runs on every render)
const component = <form
className={displayFromReaderMenu ? stylesAnnotations.annotation_edit_form : stylesAnnotations.annotation_form}
>
{displayFromReaderMenu ? <></> :
<h4>{__("reader.annotations.addNote")}</h4>
}
<div
className={classNames(displayFromReaderMenu ? "" : stylesAnnotations.annotations_line, dockedMode ? stylesAnnotations.docked_annotation_line : "")} style={{backgroundColor: !displayFromReaderMenu ? "var(--color-extralight-grey)" : ""}}>
<p>{annotationState.locatorExtended ? (annotationState.locatorExtended.selectionInfo.cleanText.length > (200-3) ? `${annotationState.locatorExtended.selectionInfo.cleanText.slice(0, 200)}...` : annotationState.locatorExtended.selectionInfo.cleanText) : ""}</p>
<TextArea id="addNote" name="addNote" wrap="hard" className={displayFromReaderMenu ? stylesAnnotations.annotation_edit_form_textarea : stylesAnnotations.annotation_form_textarea} defaultValue={annotationState.comment} ref={textAreaRef} maxLength={annotationMaxLength} onChange={(a) => setAnnotationLength(a.currentTarget.value.length)}
></TextArea>
<span style={{fontSize: "10px", color: "var(--color-medium-grey)", width: "420px", textAlign: "end"}}>{annotationLength}/{annotationMaxLength}</span>
</div>
<div className={stylesAnnotations.annotation_actions}>
{/* <div className={stylesReader.annotation_form_textarea_container}> */}
<div className={stylesAnnotations.annotation_actions_container}>
<h4>{__("reader.annotations.Color")}</h4>
<div className={stylesAnnotations.colorPicker}
role="radiogroup">
{Object.entries(annotationsColorsLight).map(([colorHex, translatorKey]) => (
<div key={`anno_color_${uuid}_${colorHex}_key`}>
<input type="radio" id={`anno_color_${uuid}_${colorHex}`} name="colorpicker" value={colorHex}
onChange={() => setColor(colorHex)}
checked={colorSelected === colorHex}
aria-label={__(translatorKey)}
/>
<label htmlFor={`anno_color_${uuid}_${colorHex}`}
style={{ backgroundColor: colorHex, border: colorSelected === colorHex ? "1px solid var(--color-dark-grey)" : "" }}
>
{colorSelected === colorHex ? <SVG ariaHidden svg={CheckIcon} /> : <></>}
</label>
</div>
),
)}
</div>
</div>
<div className={stylesAnnotations.annotation_actions_container}>
<h4>{__("reader.annotations.highlight")}</h4>
<div role="radiogroup" className={stylesAnnotations.stylePicker}>
{drawType.map((type, i) => (
<div key={type}>
<input type="radio" id={`anno_type_${uuid}_${type}`} name="drawtype" value={type}
onChange={() => setDrawType(type)}
checked={drawTypeSelected === type}
aria-label={`${__("reader.annotations.highlight")} ${type === "solid_background" ? __("reader.annotations.type.solid") : type === "outline" ? __("reader.annotations.type.outline") : type === "underline" ? __("reader.annotations.type.underline") : type === "strikethrough" ? __("reader.annotations.type.strikethrough") : __("reader.annotations.type.solid")}`}
/>
<label htmlFor={`anno_type_${uuid}_${type}`}
title={`${type === "solid_background" ? __("reader.annotations.type.solid") : type === "outline" ? __("reader.annotations.type.outline") : type === "underline" ? __("reader.annotations.type.underline") : type === "strikethrough" ? __("reader.annotations.type.strikethrough") : __("reader.annotations.type.solid")}`}
className={drawTypeSelected === type ? stylesAnnotations.drawType_active : ""}
><SVG ariaHidden svg={drawIcon[i]} /></label>
</div>
),
)}
</div>
</div>
<div className={stylesAnnotations.annotation_actions_container} style={{width: "95%"}}>
<h4>{__("catalog.tag")}</h4>
<ComboBox defaultItems={selectTagOption}
placeholder={__("catalog.addTags")}
defaultInputValue={tag}
defaultSelectedKey={selectTagOption.findIndex(({name}) => name === tag)}
selectedKey={selectTagOption.findIndex(({name}) => name === tag)}
onSelectionChange={
(key: React.Key) => {
if (key === null) {
// nothing
} else {
const found = selectTagOption.find((v) => v.id === key);
if (found)
setTag(found.name);
}
}}
svg={TagIcon}
allowsCustomValue
onInputChange={(v) => setTag(v)}
inputValue={tag}
aria-label={__("catalog.tag")}
>
{item => <ComboBoxItem>{item.name}</ComboBoxItem>}
</ComboBox>
</div>
</div>
{/* <label htmlFor="addNote">{__("reader.annotations.addNote")}</label> */}
<div className={stylesAnnotations.annotation_form_textarea_buttons}>
{displayFromReaderMenu
? <button className={stylesButtons.button_secondary_blue} aria-label={__("dialog.cancel")} onClick={cancel} type="button">{__("dialog.cancel")}</button>
: <Popover.Close className={stylesButtons.button_secondary_blue} aria-label={__("dialog.cancel")} onClick={cancel}>{__("dialog.cancel")}</Popover.Close>
}
{displayFromReaderMenu
? <button type="submit"
className={stylesButtons.button_primary_blue}
aria-label={__("reader.annotations.saveNote")}
onClick={(e) => {
e.preventDefault();
const textareaValue = textAreaRef?.current?.value || "";
const textareaNormalize = textareaValue.trim().replace(/\s*\n\s*/gm, "\0").replace(/\s\s*/g, " ").replace(/\0/g, "\n");
save(colorObj, textareaNormalize, drawTypeSelected, tag ? [tag] : []);
saveConfig();
}}
>
<SVG ariaHidden svg={SaveIcon} />
{__("reader.annotations.saveNote")}
</button>
:
<Popover.Close
type="submit"
className={stylesButtons.button_primary_blue}
aria-label={__("reader.annotations.saveNote")}
onClick={(e) => {
e.preventDefault();
const textareaValue = textAreaRef?.current?.value || "";
const textareaNormalize = textareaValue.trim().replace(/\s*\n\s*/gm, "\0").replace(/\s\s*/g, " ").replace(/\0/g, "\n");
save(colorObj, textareaNormalize, drawTypeSelected, tag ? [tag] : []);
saveConfig();
}}
>
<SVG ariaHidden svg={SaveIcon} />
{__("reader.annotations.saveNote")}
</Popover.Close>
}
</div>
{/* </div> */}
</form>;
return component;
};