2
0

editor.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. getInitialTranslation() {
  137. return [0, 0];
  138. }
  139. render() {
  140. this.div = document.createElement("div");
  141. this.div.setAttribute("data-editor-rotation", (360 - this.rotation) % 360);
  142. this.div.className = this.name;
  143. this.div.setAttribute("id", this.id);
  144. this.div.setAttribute("tabIndex", 0);
  145. this.setInForeground();
  146. this.div.addEventListener("focusin", this.#boundFocusin);
  147. this.div.addEventListener("focusout", this.#boundFocusout);
  148. const [tx, ty] = this.getInitialTranslation();
  149. this.translate(tx, ty);
  150. (0, _tools.bindEvents)(this, this.div, ["dragstart", "pointerdown"]);
  151. return this.div;
  152. }
  153. pointerdown(event) {
  154. const isMac = _tools.KeyboardManager.platform.isMac;
  155. if (event.button !== 0 || event.ctrlKey && isMac) {
  156. event.preventDefault();
  157. return;
  158. }
  159. if (event.ctrlKey && !isMac || event.shiftKey || event.metaKey && isMac) {
  160. this.parent.toggleSelected(this);
  161. } else {
  162. this.parent.setSelected(this);
  163. }
  164. this.#hasBeenSelected = true;
  165. }
  166. getRect(tx, ty) {
  167. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  168. const [pageWidth, pageHeight] = this.parent.pageDimensions;
  169. const shiftX = pageWidth * tx / parentWidth;
  170. const shiftY = pageHeight * ty / parentHeight;
  171. const x = this.x * pageWidth;
  172. const y = this.y * pageHeight;
  173. const width = this.width * pageWidth;
  174. const height = this.height * pageHeight;
  175. switch (this.rotation) {
  176. case 0:
  177. return [x + shiftX, pageHeight - y - shiftY - height, x + shiftX + width, pageHeight - y - shiftY];
  178. case 90:
  179. return [x + shiftY, pageHeight - y + shiftX, x + shiftY + height, pageHeight - y + shiftX + width];
  180. case 180:
  181. return [x - shiftX - width, pageHeight - y + shiftY, x - shiftX, pageHeight - y + shiftY + height];
  182. case 270:
  183. return [x - shiftY - height, pageHeight - y - shiftX - width, x - shiftY, pageHeight - y - shiftX];
  184. default:
  185. throw new Error("Invalid rotation");
  186. }
  187. }
  188. getRectInCurrentCoords(rect, pageHeight) {
  189. const [x1, y1, x2, y2] = rect;
  190. const width = x2 - x1;
  191. const height = y2 - y1;
  192. switch (this.rotation) {
  193. case 0:
  194. return [x1, pageHeight - y2, width, height];
  195. case 90:
  196. return [x1, pageHeight - y1, height, width];
  197. case 180:
  198. return [x2, pageHeight - y1, width, height];
  199. case 270:
  200. return [x2, pageHeight - y2, height, width];
  201. default:
  202. throw new Error("Invalid rotation");
  203. }
  204. }
  205. onceAdded() {}
  206. isEmpty() {
  207. return false;
  208. }
  209. enableEditMode() {
  210. this.#isInEditMode = true;
  211. }
  212. disableEditMode() {
  213. this.#isInEditMode = false;
  214. }
  215. isInEditMode() {
  216. return this.#isInEditMode;
  217. }
  218. shouldGetKeyboardEvents() {
  219. return false;
  220. }
  221. needsToBeRebuilt() {
  222. return this.div && !this.isAttachedToDOM;
  223. }
  224. rebuild() {
  225. this.div?.addEventListener("focusin", this.#boundFocusin);
  226. }
  227. serialize() {
  228. (0, _util.unreachable)("An editor must be serializable");
  229. }
  230. static deserialize(data, parent) {
  231. const editor = new this.prototype.constructor({
  232. parent,
  233. id: parent.getNextId()
  234. });
  235. editor.rotation = data.rotation;
  236. const [pageWidth, pageHeight] = parent.pageDimensions;
  237. const [x, y, width, height] = editor.getRectInCurrentCoords(data.rect, pageHeight);
  238. editor.x = x / pageWidth;
  239. editor.y = y / pageHeight;
  240. editor.width = width / pageWidth;
  241. editor.height = height / pageHeight;
  242. return editor;
  243. }
  244. remove() {
  245. this.div.removeEventListener("focusin", this.#boundFocusin);
  246. this.div.removeEventListener("focusout", this.#boundFocusout);
  247. if (!this.isEmpty()) {
  248. this.commit();
  249. }
  250. this.parent.remove(this);
  251. }
  252. select() {
  253. this.div?.classList.add("selectedEditor");
  254. }
  255. unselect() {
  256. this.div?.classList.remove("selectedEditor");
  257. }
  258. updateParams(type, value) {}
  259. disableEditing() {}
  260. enableEditing() {}
  261. get propertiesToUpdate() {
  262. return {};
  263. }
  264. get contentDiv() {
  265. return this.div;
  266. }
  267. get isEditing() {
  268. return this.#isEditing;
  269. }
  270. set isEditing(value) {
  271. this.#isEditing = value;
  272. if (value) {
  273. this.parent.setSelected(this);
  274. this.parent.setActiveEditor(this);
  275. } else {
  276. this.parent.setActiveEditor(null);
  277. }
  278. }
  279. }
  280. exports.AnnotationEditor = AnnotationEditor;