annotation_storage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PrintAnnotationStorage = exports.AnnotationStorage = void 0;
  27. var _util = require("../shared/util.js");
  28. var _editor = require("./editor/editor.js");
  29. var _murmurhash = require("../shared/murmurhash3.js");
  30. class AnnotationStorage {
  31. #modified = false;
  32. #storage = new Map();
  33. constructor() {
  34. this.onSetModified = null;
  35. this.onResetModified = null;
  36. this.onAnnotationEditor = null;
  37. }
  38. getValue(key, defaultValue) {
  39. const value = this.#storage.get(key);
  40. if (value === undefined) {
  41. return defaultValue;
  42. }
  43. return Object.assign(defaultValue, value);
  44. }
  45. getRawValue(key) {
  46. return this.#storage.get(key);
  47. }
  48. remove(key) {
  49. this.#storage.delete(key);
  50. if (this.#storage.size === 0) {
  51. this.resetModified();
  52. }
  53. if (typeof this.onAnnotationEditor === "function") {
  54. for (const value of this.#storage.values()) {
  55. if (value instanceof _editor.AnnotationEditor) {
  56. return;
  57. }
  58. }
  59. this.onAnnotationEditor(null);
  60. }
  61. }
  62. setValue(key, value) {
  63. const obj = this.#storage.get(key);
  64. let modified = false;
  65. if (obj !== undefined) {
  66. for (const [entry, val] of Object.entries(value)) {
  67. if (obj[entry] !== val) {
  68. modified = true;
  69. obj[entry] = val;
  70. }
  71. }
  72. } else {
  73. modified = true;
  74. this.#storage.set(key, value);
  75. }
  76. if (modified) {
  77. this.#setModified();
  78. }
  79. if (value instanceof _editor.AnnotationEditor && typeof this.onAnnotationEditor === "function") {
  80. this.onAnnotationEditor(value.constructor._type);
  81. }
  82. }
  83. has(key) {
  84. return this.#storage.has(key);
  85. }
  86. getAll() {
  87. return this.#storage.size > 0 ? (0, _util.objectFromMap)(this.#storage) : null;
  88. }
  89. get size() {
  90. return this.#storage.size;
  91. }
  92. #setModified() {
  93. if (!this.#modified) {
  94. this.#modified = true;
  95. if (typeof this.onSetModified === "function") {
  96. this.onSetModified();
  97. }
  98. }
  99. }
  100. resetModified() {
  101. if (this.#modified) {
  102. this.#modified = false;
  103. if (typeof this.onResetModified === "function") {
  104. this.onResetModified();
  105. }
  106. }
  107. }
  108. get print() {
  109. return new PrintAnnotationStorage(this);
  110. }
  111. get serializable() {
  112. if (this.#storage.size === 0) {
  113. return null;
  114. }
  115. const clone = new Map();
  116. for (const [key, val] of this.#storage) {
  117. const serialized = val instanceof _editor.AnnotationEditor ? val.serialize() : val;
  118. if (serialized) {
  119. clone.set(key, serialized);
  120. }
  121. }
  122. return clone;
  123. }
  124. static getHash(map) {
  125. if (!map) {
  126. return "";
  127. }
  128. const hash = new _murmurhash.MurmurHash3_64();
  129. for (const [key, val] of map) {
  130. hash.update(`${key}:${JSON.stringify(val)}`);
  131. }
  132. return hash.hexdigest();
  133. }
  134. }
  135. exports.AnnotationStorage = AnnotationStorage;
  136. class PrintAnnotationStorage extends AnnotationStorage {
  137. #serializable = null;
  138. constructor(parent) {
  139. super();
  140. this.#serializable = structuredClone(parent.serializable);
  141. }
  142. get print() {
  143. (0, _util.unreachable)("Should not call PrintAnnotationStorage.print");
  144. }
  145. get serializable() {
  146. return this.#serializable;
  147. }
  148. }
  149. exports.PrintAnnotationStorage = PrintAnnotationStorage;