annotation_editor_layer.js 10 KB

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