@folijs/react 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
1
  import {
2
2
  type Alignment,
3
+ acceptAllChanges,
4
+ acceptChange,
5
+ addComment,
3
6
  alignAt,
4
7
  type Command,
5
8
  canOutdent,
@@ -14,12 +17,17 @@ import {
14
17
  lineSpacingAt,
15
18
  linkAt,
16
19
  listKindAt,
20
+ nextChange,
17
21
  normalizeHref,
18
22
  openLinkEditor,
19
23
  paragraphStyleAt,
20
24
  paragraphStyles,
25
+ previousChange,
26
+ type ReviewView,
21
27
  redo,
22
28
  redoDepth,
29
+ rejectAllChanges,
30
+ rejectChange,
23
31
  removeLink,
24
32
  requestsLinkEditor,
25
33
  setAlign,
@@ -29,9 +37,12 @@ import {
29
37
  setLineSpacing,
30
38
  setLink,
31
39
  setParagraphStyle,
40
+ setReviewView,
32
41
  setTextColor,
42
+ setTrackChanges,
33
43
  toggleList,
34
44
  toggleMark,
45
+ trackState,
35
46
  undo,
36
47
  undoDepth,
37
48
  wordContext,
@@ -64,6 +75,11 @@ export interface FoliToolbarProps {
64
75
  onClickCommand?: (() => void) | undefined;
65
76
  /** Accessible name of the toolbar (default "Formatting"). */
66
77
  label?: string | undefined;
78
+ /**
79
+ * Show "New comment": only where a comments pane (`FoliComments`) lets the comment be written
80
+ * (`FoliEditor` passes it). Default false.
81
+ */
82
+ comments?: boolean | undefined;
67
83
  className?: string | undefined;
68
84
  }
69
85
 
@@ -186,6 +202,11 @@ const FONTS = [
186
202
  ];
187
203
  const SIZES = [8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
188
204
  const LINE_SPACINGS = [1, 1.15, 1.5, 2, 2.5, 3];
205
+ const REVIEW_VIEWS = [
206
+ { value: "markup", label: "All markup" },
207
+ { value: "simple", label: "Simple markup" },
208
+ { value: "final", label: "No markup" },
209
+ ];
189
210
 
190
211
  /** Word's controls. */
191
212
  const GROUPS: Item[][] = [
@@ -351,6 +372,78 @@ const GROUPS: Item[][] = [
351
372
  command: () => changeIndent(1),
352
373
  },
353
374
  ],
375
+ [
376
+ {
377
+ key: "newComment",
378
+ label: "New comment on the selection",
379
+ text: "New comment",
380
+ enabled: (state) => wordContext(state) !== null && hasTextblock(state),
381
+ command: () => addComment(""),
382
+ },
383
+ {
384
+ key: "trackChanges",
385
+ label: "Track changes",
386
+ text: "Track changes",
387
+ pressed: (state) => trackState(state)?.on === true,
388
+ enabled: (state) => trackState(state) !== null,
389
+ command: (state) => setTrackChanges(!trackState(state)?.on),
390
+ },
391
+ {
392
+ key: "reviewView",
393
+ label: "Show changes as",
394
+ text: "Show",
395
+ enabled: (state) => trackState(state) !== null,
396
+ select: {
397
+ options: () => REVIEW_VIEWS,
398
+ value: (state) => trackState(state)?.view ?? "markup",
399
+ pick: (view) => setReviewView(view as ReviewView),
400
+ },
401
+ },
402
+ // Enabled while tracking can be used: whether there is a change to go to or resolve would
403
+ // walk the whole document after every transaction.
404
+ {
405
+ key: "previousChange",
406
+ label: "Previous change",
407
+ text: "Previous change",
408
+ enabled: (state) => trackState(state) !== null,
409
+ command: () => previousChange,
410
+ },
411
+ {
412
+ key: "nextChange",
413
+ label: "Next change",
414
+ text: "Next change",
415
+ enabled: (state) => trackState(state) !== null,
416
+ command: () => nextChange,
417
+ },
418
+ {
419
+ key: "acceptChange",
420
+ label: "Accept the change here, or the changes selected",
421
+ text: "Accept",
422
+ enabled: (state) => trackState(state) !== null,
423
+ command: () => acceptChange,
424
+ },
425
+ {
426
+ key: "rejectChange",
427
+ label: "Reject the change here, or the changes selected",
428
+ text: "Reject",
429
+ enabled: (state) => trackState(state) !== null,
430
+ command: () => rejectChange,
431
+ },
432
+ {
433
+ key: "acceptAll",
434
+ label: "Accept all changes",
435
+ text: "Accept all",
436
+ enabled: (state) => trackState(state) !== null,
437
+ command: () => acceptAllChanges(),
438
+ },
439
+ {
440
+ key: "rejectAll",
441
+ label: "Reject all changes",
442
+ text: "Reject all",
443
+ enabled: (state) => trackState(state) !== null,
444
+ command: () => rejectAllChanges(),
445
+ },
446
+ ],
354
447
  [
355
448
  {
356
449
  key: "undo",
@@ -371,6 +464,9 @@ const GROUPS: Item[][] = [
371
464
  ];
372
465
 
373
466
  const ITEMS = GROUPS.flat();
467
+ /** Without "New comment": where no comments pane lets a comment be written. */
468
+ const GROUPS_NO_COMMENT = GROUPS.map((group) => group.filter((i) => i.key !== "newComment"));
469
+ const ITEMS_NO_COMMENT = GROUPS_NO_COMMENT.flat();
374
470
 
375
471
  interface LinkDialogState {
376
472
  /** The link at the selection when the dialog opened ("" for none). */
@@ -407,6 +503,7 @@ const statesOf =
407
503
  value: item.menu?.value(state) ?? item.select?.value(state),
408
504
  }));
409
505
  const selectStates = statesOf(ITEMS);
506
+ const selectStatesNoComment = statesOf(ITEMS_NO_COMMENT);
410
507
 
411
508
  /**
412
509
  * Word's formatting controls for an editor: paragraph style, font and size boxes, marks, link (a
@@ -422,11 +519,15 @@ export function FoliToolbar({
422
519
  onClickCommand,
423
520
  label = "Formatting",
424
521
  className,
522
+ comments = false,
425
523
  }: FoliToolbarProps) {
426
524
  const editor = useEditorOrContext(given, "FoliToolbar");
427
- const groups = GROUPS;
428
- const items = ITEMS;
429
- const states = useEditorState(selectStates, { editor, isEqual: sameStates });
525
+ const groups = comments ? GROUPS : GROUPS_NO_COMMENT;
526
+ const items = comments ? ITEMS : ITEMS_NO_COMMENT;
527
+ const states = useEditorState(comments ? selectStates : selectStatesNoComment, {
528
+ editor,
529
+ isEqual: sameStates,
530
+ });
430
531
  const [active, setActive] = useState(0);
431
532
  const buttons = useRef<(HTMLButtonElement | HTMLSelectElement | null)[]>([]);
432
533
  // Whether a select box was last used with the pointer (its pick then goes back to the document).
package/src/editor.tsx CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  type EditorDocument,
5
5
  type EditorState,
6
6
  type Plugin,
7
+ type ReviewView,
7
8
  } from "@folijs/core";
8
9
  import {
9
10
  createContext,
@@ -33,6 +34,12 @@ export interface UseFoliEditorOptions {
33
34
  initialContent?: EditorDocument | undefined;
34
35
  /** Read once, when the editor is created. */
35
36
  plugins?: readonly Plugin[] | undefined;
37
+ /** Read once: who edits (tracked changes are made in this name; `createEditor`'s `author`). */
38
+ author?: string | undefined;
39
+ /** Read once: record edits as tracked changes from the start (a `.docx`'s `trackRevisions`). */
40
+ trackChanges?: boolean | undefined;
41
+ /** Read once: how tracked changes show at first (default `markup`). */
42
+ reviewView?: ReviewView | undefined;
36
43
  }
37
44
 
38
45
  /**
@@ -42,8 +49,14 @@ export interface UseFoliEditorOptions {
42
49
  */
43
50
  export function useFoliEditor(options: UseFoliEditorOptions = {}): Editor {
44
51
  const [editor] = useState(() => {
45
- const { initialContent: document, plugins } = options;
46
- return createEditor({ ...(document && { document }), ...(plugins && { plugins }) });
52
+ const { initialContent: document, plugins, author, trackChanges, reviewView } = options;
53
+ return createEditor({
54
+ ...(document && { document }),
55
+ ...(plugins && { plugins }),
56
+ ...(author !== undefined && { author }),
57
+ ...(trackChanges !== undefined && { trackChanges }),
58
+ ...(reviewView !== undefined && { reviewView }),
59
+ });
47
60
  });
48
61
  return editor;
49
62
  }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export {
7
7
  useFoliEditor,
8
8
  } from "./editor.js";
9
9
  export { FoliCanvas, type FoliCanvasHandle, type FoliCanvasProps } from "./FoliCanvas.js";
10
+ export { FoliComments, type FoliCommentsProps } from "./FoliComments.js";
10
11
  export {
11
12
  type FoliChange,
12
13
  FoliEditor,
package/styles.css CHANGED
@@ -260,6 +260,104 @@
260
260
  background: var(--foli-border-color);
261
261
  }
262
262
 
263
+ /* The pages and the comments pane (FoliEditor) side by side. */
264
+ .foli-editor-body {
265
+ display: flex;
266
+ align-items: flex-start;
267
+ background: var(--foli-canvas-background);
268
+ }
269
+
270
+ .foli-editor-body > .foli-canvas {
271
+ flex: 1 1 auto;
272
+ min-width: 0;
273
+ }
274
+
275
+ /* Comments pane (FoliComments): threads beside their text. */
276
+ .foli-comments {
277
+ position: relative;
278
+ flex: 0 0 18rem;
279
+ margin: 32px 16px 32px 0;
280
+ color: var(--foli-text-color);
281
+ font:
282
+ 13px / 1.4 system-ui,
283
+ sans-serif;
284
+ }
285
+
286
+ .foli-comments-empty {
287
+ margin: 0;
288
+ opacity: 0.7;
289
+ }
290
+
291
+ .foli-comment-thread {
292
+ position: absolute;
293
+ inset-inline: 0;
294
+ padding: 8px;
295
+ border: 1px solid var(--foli-border-color);
296
+ border-radius: 6px;
297
+ background: var(--foli-page-background);
298
+ box-shadow: var(--foli-page-shadow);
299
+ }
300
+
301
+ .foli-comment-thread[data-active],
302
+ .foli-comment-thread:focus-within {
303
+ border-color: var(--foli-focus-ring);
304
+ }
305
+
306
+ .foli-comment-thread[data-done] {
307
+ opacity: 0.7;
308
+ }
309
+
310
+ .foli-comment + .foli-comment {
311
+ margin-top: 8px;
312
+ padding-top: 8px;
313
+ border-top: 1px solid var(--foli-border-color);
314
+ }
315
+
316
+ .foli-comment-meta,
317
+ .foli-comment-text p {
318
+ margin: 0;
319
+ }
320
+
321
+ .foli-comment-actions {
322
+ display: flex;
323
+ flex-wrap: wrap;
324
+ gap: 4px;
325
+ margin-top: 6px;
326
+ }
327
+
328
+ .foli-comment-actions button {
329
+ height: 28px;
330
+ padding: 0 8px;
331
+ border: 1px solid var(--foli-border-color);
332
+ border-radius: 4px;
333
+ background: none;
334
+ color: inherit;
335
+ font: inherit;
336
+ cursor: pointer;
337
+ }
338
+
339
+ .foli-comment-form label {
340
+ display: block;
341
+ margin-top: 6px;
342
+ }
343
+
344
+ .foli-comment-form textarea {
345
+ box-sizing: border-box;
346
+ width: 100%;
347
+ font: inherit;
348
+ }
349
+
350
+ .foli-comments :focus-visible {
351
+ outline: 2px solid var(--foli-focus-ring);
352
+ outline-offset: 1px;
353
+ }
354
+
355
+ /* The text of the comment that has the focus. */
356
+ .foli-content .foli-comment-active {
357
+ outline: 2px solid var(--foli-focus-ring);
358
+ outline-offset: 1px;
359
+ }
360
+
263
361
  /*
264
362
  * The view prints one sheet per page (size from the document settings); this drops the chrome.
265
363
  * Hide the rest of your page in print: anything printed before the pages adds sheets.
@@ -273,7 +371,12 @@
273
371
  box-shadow: none;
274
372
  }
275
373
  .foli-toolbar,
276
- .foli-link-dialog {
374
+ .foli-link-dialog,
375
+ .foli-comments {
277
376
  display: none;
278
377
  }
378
+ .foli-editor-body {
379
+ display: block;
380
+ background: none;
381
+ }
279
382
  }