annotation_storage.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { objectFromMap, unreachable } from "../shared/util.js";
  16. import { AnnotationEditor } from "./editor/editor.js";
  17. import { MurmurHash3_64 } from "../shared/murmurhash3.js";
  18. /**
  19. * Key/value storage for annotation data in forms.
  20. */
  21. class AnnotationStorage {
  22. #modified = false;
  23. #storage = new Map();
  24. constructor() {
  25. // Callbacks to signal when the modification state is set or reset.
  26. // This is used by the viewer to only bind on `beforeunload` if forms
  27. // are actually edited to prevent doing so unconditionally since that
  28. // can have undesirable effects.
  29. this.onSetModified = null;
  30. this.onResetModified = null;
  31. this.onAnnotationEditor = null;
  32. }
  33. /**
  34. * Get the value for a given key if it exists, or return the default value.
  35. * @param {string} key
  36. * @param {Object} defaultValue
  37. * @returns {Object}
  38. */
  39. getValue(key, defaultValue) {
  40. const value = this.#storage.get(key);
  41. if (value === undefined) {
  42. return defaultValue;
  43. }
  44. return Object.assign(defaultValue, value);
  45. }
  46. /**
  47. * Get the value for a given key.
  48. * @param {string} key
  49. * @returns {Object}
  50. */
  51. getRawValue(key) {
  52. return this.#storage.get(key);
  53. }
  54. /**
  55. * Remove a value from the storage.
  56. * @param {string} key
  57. */
  58. remove(key) {
  59. this.#storage.delete(key);
  60. if (this.#storage.size === 0) {
  61. this.resetModified();
  62. }
  63. if (typeof this.onAnnotationEditor === "function") {
  64. for (const value of this.#storage.values()) {
  65. if (value instanceof AnnotationEditor) {
  66. return;
  67. }
  68. }
  69. this.onAnnotationEditor(null);
  70. }
  71. }
  72. /**
  73. * Set the value for a given key
  74. * @param {string} key
  75. * @param {Object} value
  76. */
  77. setValue(key, value) {
  78. const obj = this.#storage.get(key);
  79. let modified = false;
  80. if (obj !== undefined) {
  81. for (const [entry, val] of Object.entries(value)) {
  82. if (obj[entry] !== val) {
  83. modified = true;
  84. obj[entry] = val;
  85. }
  86. }
  87. } else {
  88. modified = true;
  89. this.#storage.set(key, value);
  90. }
  91. if (modified) {
  92. this.#setModified();
  93. }
  94. if (
  95. value instanceof AnnotationEditor &&
  96. typeof this.onAnnotationEditor === "function"
  97. ) {
  98. this.onAnnotationEditor(value.constructor._type);
  99. }
  100. }
  101. /**
  102. * Check if the storage contains the given key.
  103. * @param {string} key
  104. * @returns {boolean}
  105. */
  106. has(key) {
  107. return this.#storage.has(key);
  108. }
  109. getAll() {
  110. return this.#storage.size > 0 ? objectFromMap(this.#storage) : null;
  111. }
  112. get size() {
  113. return this.#storage.size;
  114. }
  115. #setModified() {
  116. if (!this.#modified) {
  117. this.#modified = true;
  118. if (typeof this.onSetModified === "function") {
  119. this.onSetModified();
  120. }
  121. }
  122. }
  123. resetModified() {
  124. if (this.#modified) {
  125. this.#modified = false;
  126. if (typeof this.onResetModified === "function") {
  127. this.onResetModified();
  128. }
  129. }
  130. }
  131. /**
  132. * @returns {PrintAnnotationStorage}
  133. */
  134. get print() {
  135. return new PrintAnnotationStorage(this);
  136. }
  137. /**
  138. * PLEASE NOTE: Only intended for usage within the API itself.
  139. * @ignore
  140. */
  141. get serializable() {
  142. if (this.#storage.size === 0) {
  143. return null;
  144. }
  145. const clone = new Map();
  146. for (const [key, val] of this.#storage) {
  147. const serialized =
  148. val instanceof AnnotationEditor ? val.serialize() : val;
  149. if (serialized) {
  150. clone.set(key, serialized);
  151. }
  152. }
  153. return clone;
  154. }
  155. /**
  156. * PLEASE NOTE: Only intended for usage within the API itself.
  157. * @ignore
  158. */
  159. static getHash(map) {
  160. if (!map) {
  161. return "";
  162. }
  163. const hash = new MurmurHash3_64();
  164. for (const [key, val] of map) {
  165. hash.update(`${key}:${JSON.stringify(val)}`);
  166. }
  167. return hash.hexdigest();
  168. }
  169. }
  170. /**
  171. * A special `AnnotationStorage` for use during printing, where the serializable
  172. * data is *frozen* upon initialization, to prevent scripting from modifying its
  173. * contents. (Necessary since printing is triggered synchronously in browsers.)
  174. */
  175. class PrintAnnotationStorage extends AnnotationStorage {
  176. #serializable = null;
  177. constructor(parent) {
  178. super();
  179. // Create a *copy* of the data, since Objects are passed by reference in JS.
  180. this.#serializable = structuredClone(parent.serializable);
  181. }
  182. /**
  183. * @returns {PrintAnnotationStorage}
  184. */
  185. // eslint-disable-next-line getter-return
  186. get print() {
  187. unreachable("Should not call PrintAnnotationStorage.print");
  188. }
  189. /**
  190. * PLEASE NOTE: Only intended for usage within the API itself.
  191. * @ignore
  192. */
  193. get serializable() {
  194. return this.#serializable;
  195. }
  196. }
  197. export { AnnotationStorage, PrintAnnotationStorage };