annotation_editor_layer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.AnnotationEditorLayer = void 0;
  27. var _tools = require("./tools.js");
  28. var _util = require("../../shared/util.js");
  29. var _freetext = require("./freetext.js");
  30. var _ink = require("./ink.js");
  31. class AnnotationEditorLayer {
  32. #accessibilityManager;
  33. #allowClick = false;
  34. #boundPointerup = this.pointerup.bind(this);
  35. #boundPointerdown = this.pointerdown.bind(this);
  36. #editors = new Map();
  37. #hadPointerDown = false;
  38. #isCleaningUp = false;
  39. #uiManager;
  40. static _initialized = false;
  41. constructor(options) {
  42. if (!AnnotationEditorLayer._initialized) {
  43. AnnotationEditorLayer._initialized = true;
  44. _freetext.FreeTextEditor.initialize(options.l10n);
  45. _ink.InkEditor.initialize(options.l10n);
  46. }
  47. options.uiManager.registerEditorTypes([_freetext.FreeTextEditor, _ink.InkEditor]);
  48. this.#uiManager = options.uiManager;
  49. this.annotationStorage = options.annotationStorage;
  50. this.pageIndex = options.pageIndex;
  51. this.div = options.div;
  52. this.#accessibilityManager = options.accessibilityManager;
  53. this.#uiManager.addLayer(this);
  54. }
  55. updateToolbar(mode) {
  56. this.#uiManager.updateToolbar(mode);
  57. }
  58. updateMode(mode = this.#uiManager.getMode()) {
  59. this.#cleanup();
  60. if (mode === _util.AnnotationEditorType.INK) {
  61. this.addInkEditorIfNeeded(false);
  62. this.disableClick();
  63. } else {
  64. this.enableClick();
  65. }
  66. this.#uiManager.unselectAll();
  67. this.div.classList.toggle("freeTextEditing", mode === _util.AnnotationEditorType.FREETEXT);
  68. this.div.classList.toggle("inkEditing", mode === _util.AnnotationEditorType.INK);
  69. }
  70. addInkEditorIfNeeded(isCommitting) {
  71. if (!isCommitting && this.#uiManager.getMode() !== _util.AnnotationEditorType.INK) {
  72. return;
  73. }
  74. if (!isCommitting) {
  75. for (const editor of this.#editors.values()) {
  76. if (editor.isEmpty()) {
  77. editor.setInBackground();
  78. return;
  79. }
  80. }
  81. }
  82. const editor = this.#createAndAddNewEditor({
  83. offsetX: 0,
  84. offsetY: 0
  85. });
  86. editor.setInBackground();
  87. }
  88. setEditingState(isEditing) {
  89. this.#uiManager.setEditingState(isEditing);
  90. }
  91. addCommands(params) {
  92. this.#uiManager.addCommands(params);
  93. }
  94. enable() {
  95. this.div.style.pointerEvents = "auto";
  96. for (const editor of this.#editors.values()) {
  97. editor.enableEditing();
  98. }
  99. }
  100. disable() {
  101. this.div.style.pointerEvents = "none";
  102. for (const editor of this.#editors.values()) {
  103. editor.disableEditing();
  104. }
  105. }
  106. setActiveEditor(editor) {
  107. const currentActive = this.#uiManager.getActive();
  108. if (currentActive === editor) {
  109. return;
  110. }
  111. this.#uiManager.setActiveEditor(editor);
  112. }
  113. enableClick() {
  114. this.div.addEventListener("pointerdown", this.#boundPointerdown);
  115. this.div.addEventListener("pointerup", this.#boundPointerup);
  116. }
  117. disableClick() {
  118. this.div.removeEventListener("pointerdown", this.#boundPointerdown);
  119. this.div.removeEventListener("pointerup", this.#boundPointerup);
  120. }
  121. attach(editor) {
  122. this.#editors.set(editor.id, editor);
  123. }
  124. detach(editor) {
  125. this.#editors.delete(editor.id);
  126. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  127. }
  128. remove(editor) {
  129. this.#uiManager.removeEditor(editor);
  130. this.detach(editor);
  131. this.annotationStorage.remove(editor.id);
  132. editor.div.style.display = "none";
  133. setTimeout(() => {
  134. editor.div.style.display = "";
  135. editor.div.remove();
  136. editor.isAttachedToDOM = false;
  137. if (document.activeElement === document.body) {
  138. this.#uiManager.focusMainContainer();
  139. }
  140. }, 0);
  141. if (!this.#isCleaningUp) {
  142. this.addInkEditorIfNeeded(false);
  143. }
  144. }
  145. #changeParent(editor) {
  146. if (editor.parent === this) {
  147. return;
  148. }
  149. this.attach(editor);
  150. editor.pageIndex = this.pageIndex;
  151. editor.parent?.detach(editor);
  152. editor.parent = this;
  153. if (editor.div && editor.isAttachedToDOM) {
  154. editor.div.remove();
  155. this.div.append(editor.div);
  156. }
  157. }
  158. add(editor) {
  159. this.#changeParent(editor);
  160. this.#uiManager.addEditor(editor);
  161. this.attach(editor);
  162. if (!editor.isAttachedToDOM) {
  163. const div = editor.render();
  164. this.div.append(div);
  165. editor.isAttachedToDOM = true;
  166. }
  167. this.moveEditorInDOM(editor);
  168. editor.onceAdded();
  169. this.addToAnnotationStorage(editor);
  170. }
  171. moveEditorInDOM(editor) {
  172. this.#accessibilityManager?.moveElementInDOM(this.div, editor.div, editor.contentDiv, true);
  173. }
  174. addToAnnotationStorage(editor) {
  175. if (!editor.isEmpty() && !this.annotationStorage.has(editor.id)) {
  176. this.annotationStorage.setValue(editor.id, editor);
  177. }
  178. }
  179. addOrRebuild(editor) {
  180. if (editor.needsToBeRebuilt()) {
  181. editor.rebuild();
  182. } else {
  183. this.add(editor);
  184. }
  185. }
  186. addANewEditor(editor) {
  187. const cmd = () => {
  188. this.addOrRebuild(editor);
  189. };
  190. const undo = () => {
  191. editor.remove();
  192. };
  193. this.addCommands({
  194. cmd,
  195. undo,
  196. mustExec: true
  197. });
  198. }
  199. addUndoableEditor(editor) {
  200. const cmd = () => {
  201. this.addOrRebuild(editor);
  202. };
  203. const undo = () => {
  204. editor.remove();
  205. };
  206. this.addCommands({
  207. cmd,
  208. undo,
  209. mustExec: false
  210. });
  211. }
  212. getNextId() {
  213. return this.#uiManager.getId();
  214. }
  215. #createNewEditor(params) {
  216. switch (this.#uiManager.getMode()) {
  217. case _util.AnnotationEditorType.FREETEXT:
  218. return new _freetext.FreeTextEditor(params);
  219. case _util.AnnotationEditorType.INK:
  220. return new _ink.InkEditor(params);
  221. }
  222. return null;
  223. }
  224. deserialize(data) {
  225. switch (data.annotationType) {
  226. case _util.AnnotationEditorType.FREETEXT:
  227. return _freetext.FreeTextEditor.deserialize(data, this);
  228. case _util.AnnotationEditorType.INK:
  229. return _ink.InkEditor.deserialize(data, this);
  230. }
  231. return null;
  232. }
  233. #createAndAddNewEditor(event) {
  234. const id = this.getNextId();
  235. const editor = this.#createNewEditor({
  236. parent: this,
  237. id,
  238. x: event.offsetX,
  239. y: event.offsetY
  240. });
  241. if (editor) {
  242. this.add(editor);
  243. }
  244. return editor;
  245. }
  246. setSelected(editor) {
  247. this.#uiManager.setSelected(editor);
  248. }
  249. toggleSelected(editor) {
  250. this.#uiManager.toggleSelected(editor);
  251. }
  252. isSelected(editor) {
  253. return this.#uiManager.isSelected(editor);
  254. }
  255. unselect(editor) {
  256. this.#uiManager.unselect(editor);
  257. }
  258. pointerup(event) {
  259. const isMac = _tools.KeyboardManager.platform.isMac;
  260. if (event.button !== 0 || event.ctrlKey && isMac) {
  261. return;
  262. }
  263. if (event.target !== this.div) {
  264. return;
  265. }
  266. if (!this.#hadPointerDown) {
  267. return;
  268. }
  269. this.#hadPointerDown = false;
  270. if (!this.#allowClick) {
  271. this.#allowClick = true;
  272. return;
  273. }
  274. this.#createAndAddNewEditor(event);
  275. }
  276. pointerdown(event) {
  277. const isMac = _tools.KeyboardManager.platform.isMac;
  278. if (event.button !== 0 || event.ctrlKey && isMac) {
  279. return;
  280. }
  281. if (event.target !== this.div) {
  282. return;
  283. }
  284. this.#hadPointerDown = true;
  285. const editor = this.#uiManager.getActive();
  286. this.#allowClick = !editor || editor.isEmpty();
  287. }
  288. drop(event) {
  289. const id = event.dataTransfer.getData("text/plain");
  290. const editor = this.#uiManager.getEditor(id);
  291. if (!editor) {
  292. return;
  293. }
  294. event.preventDefault();
  295. event.dataTransfer.dropEffect = "move";
  296. this.#changeParent(editor);
  297. const rect = this.div.getBoundingClientRect();
  298. const endX = event.clientX - rect.x;
  299. const endY = event.clientY - rect.y;
  300. editor.translate(endX - editor.startX, endY - editor.startY);
  301. this.moveEditorInDOM(editor);
  302. editor.div.focus();
  303. }
  304. dragover(event) {
  305. event.preventDefault();
  306. }
  307. destroy() {
  308. if (this.#uiManager.getActive()?.parent === this) {
  309. this.#uiManager.setActiveEditor(null);
  310. }
  311. for (const editor of this.#editors.values()) {
  312. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  313. editor.isAttachedToDOM = false;
  314. editor.div.remove();
  315. editor.parent = null;
  316. }
  317. this.div = null;
  318. this.#editors.clear();
  319. this.#uiManager.removeLayer(this);
  320. }
  321. #cleanup() {
  322. this.#isCleaningUp = true;
  323. for (const editor of this.#editors.values()) {
  324. if (editor.isEmpty()) {
  325. editor.remove();
  326. }
  327. }
  328. this.#isCleaningUp = false;
  329. }
  330. render(parameters) {
  331. this.viewport = parameters.viewport;
  332. (0, _tools.bindEvents)(this, this.div, ["dragover", "drop"]);
  333. this.setDimensions();
  334. for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
  335. this.add(editor);
  336. }
  337. this.updateMode();
  338. }
  339. update(parameters) {
  340. this.#uiManager.commitOrRemove();
  341. this.viewport = parameters.viewport;
  342. this.setDimensions();
  343. this.updateMode();
  344. }
  345. get scaleFactor() {
  346. return this.viewport.scale;
  347. }
  348. get pageDimensions() {
  349. const [pageLLx, pageLLy, pageURx, pageURy] = this.viewport.viewBox;
  350. const width = pageURx - pageLLx;
  351. const height = pageURy - pageLLy;
  352. return [width, height];
  353. }
  354. get viewportBaseDimensions() {
  355. const {
  356. width,
  357. height,
  358. rotation
  359. } = this.viewport;
  360. return rotation % 180 === 0 ? [width, height] : [height, width];
  361. }
  362. setDimensions() {
  363. const {
  364. width,
  365. height,
  366. rotation
  367. } = this.viewport;
  368. const flipOrientation = rotation % 180 !== 0,
  369. widthStr = Math.floor(width) + "px",
  370. heightStr = Math.floor(height) + "px";
  371. this.div.style.width = flipOrientation ? heightStr : widthStr;
  372. this.div.style.height = flipOrientation ? widthStr : heightStr;
  373. this.div.setAttribute("data-main-rotation", rotation);
  374. }
  375. }
  376. exports.AnnotationEditorLayer = AnnotationEditorLayer;