editor.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.AnnotationEditor = void 0;
  27. var _tools = require("./tools.js");
  28. var _util = require("../../shared/util.js");
  29. class AnnotationEditor {
  30. #boundFocusin = this.focusin.bind(this);
  31. #boundFocusout = this.focusout.bind(this);
  32. #hasBeenSelected = false;
  33. #isEditing = false;
  34. #isInEditMode = false;
  35. #zIndex = AnnotationEditor._zIndex++;
  36. static _colorManager = new _tools.ColorManager();
  37. static _zIndex = 1;
  38. constructor(parameters) {
  39. if (this.constructor === AnnotationEditor) {
  40. (0, _util.unreachable)("Cannot initialize AnnotationEditor.");
  41. }
  42. this.parent = parameters.parent;
  43. this.id = parameters.id;
  44. this.width = this.height = null;
  45. this.pageIndex = parameters.parent.pageIndex;
  46. this.name = parameters.name;
  47. this.div = null;
  48. const [width, height] = this.parent.viewportBaseDimensions;
  49. this.x = parameters.x / width;
  50. this.y = parameters.y / height;
  51. this.rotation = this.parent.viewport.rotation;
  52. this.isAttachedToDOM = false;
  53. }
  54. static get _defaultLineColor() {
  55. return (0, _util.shadow)(this, "_defaultLineColor", this._colorManager.getHexCode("CanvasText"));
  56. }
  57. setInBackground() {
  58. this.div.style.zIndex = 0;
  59. }
  60. setInForeground() {
  61. this.div.style.zIndex = this.#zIndex;
  62. }
  63. focusin(event) {
  64. if (!this.#hasBeenSelected) {
  65. this.parent.setSelected(this);
  66. } else {
  67. this.#hasBeenSelected = false;
  68. }
  69. }
  70. focusout(event) {
  71. if (!this.isAttachedToDOM) {
  72. return;
  73. }
  74. const target = event.relatedTarget;
  75. if (target?.closest(`#${this.id}`)) {
  76. return;
  77. }
  78. event.preventDefault();
  79. if (!this.parent.isMultipleSelection) {
  80. this.commitOrRemove();
  81. }
  82. }
  83. commitOrRemove() {
  84. if (this.isEmpty()) {
  85. this.remove();
  86. } else {
  87. this.commit();
  88. }
  89. }
  90. commit() {
  91. this.parent.addToAnnotationStorage(this);
  92. }
  93. dragstart(event) {
  94. const rect = this.parent.div.getBoundingClientRect();
  95. this.startX = event.clientX - rect.x;
  96. this.startY = event.clientY - rect.y;
  97. event.dataTransfer.setData("text/plain", this.id);
  98. event.dataTransfer.effectAllowed = "move";
  99. }
  100. setAt(x, y, tx, ty) {
  101. const [width, height] = this.parent.viewportBaseDimensions;
  102. [tx, ty] = this.screenToPageTranslation(tx, ty);
  103. this.x = (x + tx) / width;
  104. this.y = (y + ty) / height;
  105. this.div.style.left = `${100 * this.x}%`;
  106. this.div.style.top = `${100 * this.y}%`;
  107. }
  108. translate(x, y) {
  109. const [width, height] = this.parent.viewportBaseDimensions;
  110. [x, y] = this.screenToPageTranslation(x, y);
  111. this.x += x / width;
  112. this.y += y / height;
  113. this.div.style.left = `${100 * this.x}%`;
  114. this.div.style.top = `${100 * this.y}%`;
  115. }
  116. screenToPageTranslation(x, y) {
  117. const {
  118. rotation
  119. } = this.parent.viewport;
  120. switch (rotation) {
  121. case 90:
  122. return [y, -x];
  123. case 180:
  124. return [-x, -y];
  125. case 270:
  126. return [-y, x];
  127. default:
  128. return [x, y];
  129. }
  130. }
  131. setDims(width, height) {
  132. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  133. this.div.style.width = `${100 * width / parentWidth}%`;
  134. this.div.style.height = `${100 * height / parentHeight}%`;
  135. }
  136. fixDims() {
  137. const {
  138. style
  139. } = this.div;
  140. const {
  141. height,
  142. width
  143. } = style;
  144. const widthPercent = width.endsWith("%");
  145. const heightPercent = height.endsWith("%");
  146. if (widthPercent && heightPercent) {
  147. return;
  148. }
  149. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  150. if (!widthPercent) {
  151. style.width = `${100 * parseFloat(width) / parentWidth}%`;
  152. }
  153. if (!heightPercent) {
  154. style.height = `${100 * parseFloat(height) / parentHeight}%`;
  155. }
  156. }
  157. getInitialTranslation() {
  158. return [0, 0];
  159. }
  160. render() {
  161. this.div = document.createElement("div");
  162. this.div.setAttribute("data-editor-rotation", (360 - this.rotation) % 360);
  163. this.div.className = this.name;
  164. this.div.setAttribute("id", this.id);
  165. this.div.setAttribute("tabIndex", 0);
  166. this.setInForeground();
  167. this.div.addEventListener("focusin", this.#boundFocusin);
  168. this.div.addEventListener("focusout", this.#boundFocusout);
  169. const [tx, ty] = this.getInitialTranslation();
  170. this.translate(tx, ty);
  171. (0, _tools.bindEvents)(this, this.div, ["dragstart", "pointerdown"]);
  172. return this.div;
  173. }
  174. pointerdown(event) {
  175. const isMac = _tools.KeyboardManager.platform.isMac;
  176. if (event.button !== 0 || event.ctrlKey && isMac) {
  177. event.preventDefault();
  178. return;
  179. }
  180. if (event.ctrlKey && !isMac || event.shiftKey || event.metaKey && isMac) {
  181. this.parent.toggleSelected(this);
  182. } else {
  183. this.parent.setSelected(this);
  184. }
  185. this.#hasBeenSelected = true;
  186. }
  187. getRect(tx, ty) {
  188. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  189. const [pageWidth, pageHeight] = this.parent.pageDimensions;
  190. const shiftX = pageWidth * tx / parentWidth;
  191. const shiftY = pageHeight * ty / parentHeight;
  192. const x = this.x * pageWidth;
  193. const y = this.y * pageHeight;
  194. const width = this.width * pageWidth;
  195. const height = this.height * pageHeight;
  196. switch (this.rotation) {
  197. case 0:
  198. return [x + shiftX, pageHeight - y - shiftY - height, x + shiftX + width, pageHeight - y - shiftY];
  199. case 90:
  200. return [x + shiftY, pageHeight - y + shiftX, x + shiftY + height, pageHeight - y + shiftX + width];
  201. case 180:
  202. return [x - shiftX - width, pageHeight - y + shiftY, x - shiftX, pageHeight - y + shiftY + height];
  203. case 270:
  204. return [x - shiftY - height, pageHeight - y - shiftX - width, x - shiftY, pageHeight - y - shiftX];
  205. default:
  206. throw new Error("Invalid rotation");
  207. }
  208. }
  209. getRectInCurrentCoords(rect, pageHeight) {
  210. const [x1, y1, x2, y2] = rect;
  211. const width = x2 - x1;
  212. const height = y2 - y1;
  213. switch (this.rotation) {
  214. case 0:
  215. return [x1, pageHeight - y2, width, height];
  216. case 90:
  217. return [x1, pageHeight - y1, height, width];
  218. case 180:
  219. return [x2, pageHeight - y1, width, height];
  220. case 270:
  221. return [x2, pageHeight - y2, height, width];
  222. default:
  223. throw new Error("Invalid rotation");
  224. }
  225. }
  226. onceAdded() {}
  227. isEmpty() {
  228. return false;
  229. }
  230. enableEditMode() {
  231. this.#isInEditMode = true;
  232. }
  233. disableEditMode() {
  234. this.#isInEditMode = false;
  235. }
  236. isInEditMode() {
  237. return this.#isInEditMode;
  238. }
  239. shouldGetKeyboardEvents() {
  240. return false;
  241. }
  242. needsToBeRebuilt() {
  243. return this.div && !this.isAttachedToDOM;
  244. }
  245. rebuild() {
  246. this.div?.addEventListener("focusin", this.#boundFocusin);
  247. }
  248. serialize() {
  249. (0, _util.unreachable)("An editor must be serializable");
  250. }
  251. static deserialize(data, parent) {
  252. const editor = new this.prototype.constructor({
  253. parent,
  254. id: parent.getNextId()
  255. });
  256. editor.rotation = data.rotation;
  257. const [pageWidth, pageHeight] = parent.pageDimensions;
  258. const [x, y, width, height] = editor.getRectInCurrentCoords(data.rect, pageHeight);
  259. editor.x = x / pageWidth;
  260. editor.y = y / pageHeight;
  261. editor.width = width / pageWidth;
  262. editor.height = height / pageHeight;
  263. return editor;
  264. }
  265. remove() {
  266. this.div.removeEventListener("focusin", this.#boundFocusin);
  267. this.div.removeEventListener("focusout", this.#boundFocusout);
  268. if (!this.isEmpty()) {
  269. this.commit();
  270. }
  271. this.parent.remove(this);
  272. }
  273. select() {
  274. this.div?.classList.add("selectedEditor");
  275. }
  276. unselect() {
  277. this.div?.classList.remove("selectedEditor");
  278. }
  279. updateParams(type, value) {}
  280. disableEditing() {}
  281. enableEditing() {}
  282. get propertiesToUpdate() {
  283. return {};
  284. }
  285. get contentDiv() {
  286. return this.div;
  287. }
  288. get isEditing() {
  289. return this.#isEditing;
  290. }
  291. set isEditing(value) {
  292. this.#isEditing = value;
  293. if (value) {
  294. this.parent.setSelected(this);
  295. this.parent.setActiveEditor(this);
  296. } else {
  297. this.parent.setActiveEditor(null);
  298. }
  299. }
  300. }
  301. exports.AnnotationEditor = AnnotationEditor;