annotation_editor_layer.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 _util = require("../../shared/util.js");
  28. var _tools = require("./tools.js");
  29. var _freetext = require("./freetext.js");
  30. var _ink = require("./ink.js");
  31. var _display_utils = require("../display_utils.js");
  32. class AnnotationEditorLayer {
  33. #accessibilityManager;
  34. #allowClick = false;
  35. #boundPointerup = this.pointerup.bind(this);
  36. #boundPointerdown = this.pointerdown.bind(this);
  37. #editors = new Map();
  38. #hadPointerDown = false;
  39. #isCleaningUp = false;
  40. #uiManager;
  41. static _initialized = false;
  42. constructor(options) {
  43. if (!AnnotationEditorLayer._initialized) {
  44. AnnotationEditorLayer._initialized = true;
  45. _freetext.FreeTextEditor.initialize(options.l10n);
  46. _ink.InkEditor.initialize(options.l10n);
  47. }
  48. options.uiManager.registerEditorTypes([_freetext.FreeTextEditor, _ink.InkEditor]);
  49. this.#uiManager = options.uiManager;
  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. editor.div.style.display = "none";
  132. setTimeout(() => {
  133. editor.div.style.display = "";
  134. editor.div.remove();
  135. editor.isAttachedToDOM = false;
  136. if (document.activeElement === document.body) {
  137. this.#uiManager.focusMainContainer();
  138. }
  139. }, 0);
  140. if (!this.#isCleaningUp) {
  141. this.addInkEditorIfNeeded(false);
  142. }
  143. }
  144. #changeParent(editor) {
  145. if (editor.parent === this) {
  146. return;
  147. }
  148. this.attach(editor);
  149. editor.parent?.detach(editor);
  150. editor.setParent(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.#uiManager.addToAnnotationStorage(editor);
  168. }
  169. moveEditorInDOM(editor) {
  170. this.#accessibilityManager?.moveElementInDOM(this.div, editor.div, editor.contentDiv, true);
  171. }
  172. addOrRebuild(editor) {
  173. if (editor.needsToBeRebuilt()) {
  174. editor.rebuild();
  175. } else {
  176. this.add(editor);
  177. }
  178. }
  179. addANewEditor(editor) {
  180. const cmd = () => {
  181. this.addOrRebuild(editor);
  182. };
  183. const undo = () => {
  184. editor.remove();
  185. };
  186. this.addCommands({
  187. cmd,
  188. undo,
  189. mustExec: true
  190. });
  191. }
  192. addUndoableEditor(editor) {
  193. const cmd = () => {
  194. this.addOrRebuild(editor);
  195. };
  196. const undo = () => {
  197. editor.remove();
  198. };
  199. this.addCommands({
  200. cmd,
  201. undo,
  202. mustExec: false
  203. });
  204. }
  205. getNextId() {
  206. return this.#uiManager.getId();
  207. }
  208. #createNewEditor(params) {
  209. switch (this.#uiManager.getMode()) {
  210. case _util.AnnotationEditorType.FREETEXT:
  211. return new _freetext.FreeTextEditor(params);
  212. case _util.AnnotationEditorType.INK:
  213. return new _ink.InkEditor(params);
  214. }
  215. return null;
  216. }
  217. deserialize(data) {
  218. switch (data.annotationType) {
  219. case _util.AnnotationEditorType.FREETEXT:
  220. return _freetext.FreeTextEditor.deserialize(data, this, this.#uiManager);
  221. case _util.AnnotationEditorType.INK:
  222. return _ink.InkEditor.deserialize(data, this, this.#uiManager);
  223. }
  224. return null;
  225. }
  226. #createAndAddNewEditor(event) {
  227. const id = this.getNextId();
  228. const editor = this.#createNewEditor({
  229. parent: this,
  230. id,
  231. x: event.offsetX,
  232. y: event.offsetY,
  233. uiManager: this.#uiManager
  234. });
  235. if (editor) {
  236. this.add(editor);
  237. }
  238. return editor;
  239. }
  240. setSelected(editor) {
  241. this.#uiManager.setSelected(editor);
  242. }
  243. toggleSelected(editor) {
  244. this.#uiManager.toggleSelected(editor);
  245. }
  246. isSelected(editor) {
  247. return this.#uiManager.isSelected(editor);
  248. }
  249. unselect(editor) {
  250. this.#uiManager.unselect(editor);
  251. }
  252. pointerup(event) {
  253. const {
  254. isMac
  255. } = _util.FeatureTest.platform;
  256. if (event.button !== 0 || event.ctrlKey && isMac) {
  257. return;
  258. }
  259. if (event.target !== this.div) {
  260. return;
  261. }
  262. if (!this.#hadPointerDown) {
  263. return;
  264. }
  265. this.#hadPointerDown = false;
  266. if (!this.#allowClick) {
  267. this.#allowClick = true;
  268. return;
  269. }
  270. this.#createAndAddNewEditor(event);
  271. }
  272. pointerdown(event) {
  273. const {
  274. isMac
  275. } = _util.FeatureTest.platform;
  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.setParent(null);
  312. editor.isAttachedToDOM = false;
  313. editor.div.remove();
  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({
  329. viewport
  330. }) {
  331. this.viewport = viewport;
  332. (0, _display_utils.setLayerDimensions)(this.div, viewport);
  333. (0, _tools.bindEvents)(this, this.div, ["dragover", "drop"]);
  334. for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
  335. this.add(editor);
  336. }
  337. this.updateMode();
  338. }
  339. update({
  340. viewport
  341. }) {
  342. this.#uiManager.commitOrRemove();
  343. this.viewport = viewport;
  344. (0, _display_utils.setLayerDimensions)(this.div, {
  345. rotation: viewport.rotation
  346. });
  347. this.updateMode();
  348. }
  349. get pageDimensions() {
  350. const {
  351. pageWidth,
  352. pageHeight
  353. } = this.viewport.rawDims;
  354. return [pageWidth, pageHeight];
  355. }
  356. }
  357. exports.AnnotationEditorLayer = AnnotationEditorLayer;