@erdemtuna/doc-review 0.7.0 → 0.8.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.
@@ -0,0 +1,59 @@
1
+ export const REVIEW_MODES = Object.freeze(["view", "edit"]);
2
+
3
+ export function normalizeReviewMode(mode) {
4
+ return mode === "edit" ? "edit" : "view";
5
+ }
6
+
7
+ export function savePolicyForPage(page) {
8
+ return page && (page.kind === "url" || page.markdown || page.feedbackOnly)
9
+ ? "feedback-only"
10
+ : "writable";
11
+ }
12
+
13
+ export function reviewConfiguration(page, mode) {
14
+ return {
15
+ mode: normalizeReviewMode(mode),
16
+ savePolicy: savePolicyForPage(page),
17
+ };
18
+ }
19
+
20
+ export function applyBodyReviewMode(body, mode) {
21
+ const normalized = normalizeReviewMode(mode);
22
+ if (normalized === "edit") {
23
+ if (body.getAttribute("contenteditable") !== "true") body.setAttribute("contenteditable", "true");
24
+ if (body.getAttribute("spellcheck") !== "false") body.setAttribute("spellcheck", "false");
25
+ } else {
26
+ if (body.hasAttribute("contenteditable")) body.removeAttribute("contenteditable");
27
+ if (body.hasAttribute("spellcheck")) body.removeAttribute("spellcheck");
28
+ }
29
+ return normalized;
30
+ }
31
+
32
+ /** Keep the active mode intact when an application hydrates and rewrites body attributes. */
33
+ export function keepBodyInReviewMode(body, initialMode = "view") {
34
+ let mode = normalizeReviewMode(initialMode);
35
+ let applying = false;
36
+ const enforce = () => {
37
+ if (applying) return;
38
+ applying = true;
39
+ applyBodyReviewMode(body, mode);
40
+ applying = false;
41
+ };
42
+ const Observer = body.ownerDocument.defaultView.MutationObserver;
43
+ const observer = new Observer(enforce);
44
+ observer.observe(body, { attributes: true, attributeFilter: ["contenteditable", "spellcheck"] });
45
+ enforce();
46
+ return {
47
+ get mode() {
48
+ return mode;
49
+ },
50
+ setMode(nextMode) {
51
+ mode = normalizeReviewMode(nextMode);
52
+ enforce();
53
+ return mode;
54
+ },
55
+ disconnect() {
56
+ observer.disconnect();
57
+ },
58
+ };
59
+ }