annotation_storage.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. constructor() {
  32. this._storage = new Map();
  33. this._modified = false;
  34. this.onSetModified = null;
  35. this.onResetModified = null;
  36. }
  37. getValue(key, defaultValue) {
  38. const value = this._storage.get(key);
  39. if (value === undefined) {
  40. return defaultValue;
  41. }
  42. return Object.assign(defaultValue, value);
  43. }
  44. getRawValue(key) {
  45. return this._storage.get(key);
  46. }
  47. removeKey(key) {
  48. this._storage.delete(key);
  49. if (this._storage.size === 0) {
  50. this.resetModified();
  51. }
  52. }
  53. setValue(key, value) {
  54. const obj = this._storage.get(key);
  55. let modified = false;
  56. if (obj !== undefined) {
  57. for (const [entry, val] of Object.entries(value)) {
  58. if (obj[entry] !== val) {
  59. modified = true;
  60. obj[entry] = val;
  61. }
  62. }
  63. } else {
  64. modified = true;
  65. this._storage.set(key, value);
  66. }
  67. if (modified) {
  68. this.#setModified();
  69. }
  70. }
  71. has(key) {
  72. return this._storage.has(key);
  73. }
  74. getAll() {
  75. return this._storage.size > 0 ? (0, _util.objectFromMap)(this._storage) : null;
  76. }
  77. get size() {
  78. return this._storage.size;
  79. }
  80. #setModified() {
  81. if (!this._modified) {
  82. this._modified = true;
  83. if (typeof this.onSetModified === "function") {
  84. this.onSetModified();
  85. }
  86. }
  87. }
  88. resetModified() {
  89. if (this._modified) {
  90. this._modified = false;
  91. if (typeof this.onResetModified === "function") {
  92. this.onResetModified();
  93. }
  94. }
  95. }
  96. get print() {
  97. return new PrintAnnotationStorage(this);
  98. }
  99. get serializable() {
  100. if (this._storage.size === 0) {
  101. return null;
  102. }
  103. const clone = new Map();
  104. for (const [key, val] of this._storage) {
  105. const serialized = val instanceof _editor.AnnotationEditor ? val.serialize() : val;
  106. if (serialized) {
  107. clone.set(key, serialized);
  108. }
  109. }
  110. return clone;
  111. }
  112. static getHash(map) {
  113. if (!map) {
  114. return "";
  115. }
  116. const hash = new _murmurhash.MurmurHash3_64();
  117. for (const [key, val] of map) {
  118. hash.update(`${key}:${JSON.stringify(val)}`);
  119. }
  120. return hash.hexdigest();
  121. }
  122. }
  123. exports.AnnotationStorage = AnnotationStorage;
  124. class PrintAnnotationStorage extends AnnotationStorage {
  125. #serializable = null;
  126. constructor(parent) {
  127. super();
  128. this.#serializable = structuredClone(parent.serializable);
  129. }
  130. get print() {
  131. (0, _util.unreachable)("Should not call PrintAnnotationStorage.print");
  132. }
  133. get serializable() {
  134. return this.#serializable;
  135. }
  136. }
  137. exports.PrintAnnotationStorage = PrintAnnotationStorage;