annotation_layer.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.AnnotationLayer = void 0;
  27. var _util = require("../shared/util.js");
  28. var _display_utils = require("./display_utils.js");
  29. var _annotation_storage = require("./annotation_storage.js");
  30. var _scripting_utils = require("../shared/scripting_utils.js");
  31. var _xfa_layer = require("./xfa_layer.js");
  32. const DEFAULT_TAB_INDEX = 1000;
  33. const GetElementsByNameSet = new WeakSet();
  34. class AnnotationElementFactory {
  35. static create(parameters) {
  36. const subtype = parameters.data.annotationType;
  37. switch (subtype) {
  38. case _util.AnnotationType.LINK:
  39. return new LinkAnnotationElement(parameters);
  40. case _util.AnnotationType.TEXT:
  41. return new TextAnnotationElement(parameters);
  42. case _util.AnnotationType.WIDGET:
  43. const fieldType = parameters.data.fieldType;
  44. switch (fieldType) {
  45. case "Tx":
  46. return new TextWidgetAnnotationElement(parameters);
  47. case "Btn":
  48. if (parameters.data.radioButton) {
  49. return new RadioButtonWidgetAnnotationElement(parameters);
  50. } else if (parameters.data.checkBox) {
  51. return new CheckboxWidgetAnnotationElement(parameters);
  52. }
  53. return new PushButtonWidgetAnnotationElement(parameters);
  54. case "Ch":
  55. return new ChoiceWidgetAnnotationElement(parameters);
  56. }
  57. return new WidgetAnnotationElement(parameters);
  58. case _util.AnnotationType.POPUP:
  59. return new PopupAnnotationElement(parameters);
  60. case _util.AnnotationType.FREETEXT:
  61. return new FreeTextAnnotationElement(parameters);
  62. case _util.AnnotationType.LINE:
  63. return new LineAnnotationElement(parameters);
  64. case _util.AnnotationType.SQUARE:
  65. return new SquareAnnotationElement(parameters);
  66. case _util.AnnotationType.CIRCLE:
  67. return new CircleAnnotationElement(parameters);
  68. case _util.AnnotationType.POLYLINE:
  69. return new PolylineAnnotationElement(parameters);
  70. case _util.AnnotationType.CARET:
  71. return new CaretAnnotationElement(parameters);
  72. case _util.AnnotationType.INK:
  73. return new InkAnnotationElement(parameters);
  74. case _util.AnnotationType.POLYGON:
  75. return new PolygonAnnotationElement(parameters);
  76. case _util.AnnotationType.HIGHLIGHT:
  77. return new HighlightAnnotationElement(parameters);
  78. case _util.AnnotationType.UNDERLINE:
  79. return new UnderlineAnnotationElement(parameters);
  80. case _util.AnnotationType.SQUIGGLY:
  81. return new SquigglyAnnotationElement(parameters);
  82. case _util.AnnotationType.STRIKEOUT:
  83. return new StrikeOutAnnotationElement(parameters);
  84. case _util.AnnotationType.STAMP:
  85. return new StampAnnotationElement(parameters);
  86. case _util.AnnotationType.FILEATTACHMENT:
  87. return new FileAttachmentAnnotationElement(parameters);
  88. default:
  89. return new AnnotationElement(parameters);
  90. }
  91. }
  92. }
  93. class AnnotationElement {
  94. constructor(parameters, {
  95. isRenderable = false,
  96. ignoreBorder = false,
  97. createQuadrilaterals = false
  98. } = {}) {
  99. this.isRenderable = isRenderable;
  100. this.data = parameters.data;
  101. this.layer = parameters.layer;
  102. this.page = parameters.page;
  103. this.viewport = parameters.viewport;
  104. this.linkService = parameters.linkService;
  105. this.downloadManager = parameters.downloadManager;
  106. this.imageResourcesPath = parameters.imageResourcesPath;
  107. this.renderForms = parameters.renderForms;
  108. this.svgFactory = parameters.svgFactory;
  109. this.annotationStorage = parameters.annotationStorage;
  110. this.enableScripting = parameters.enableScripting;
  111. this.hasJSActions = parameters.hasJSActions;
  112. this._fieldObjects = parameters.fieldObjects;
  113. this._mouseState = parameters.mouseState;
  114. if (isRenderable) {
  115. this.container = this._createContainer(ignoreBorder);
  116. }
  117. if (createQuadrilaterals) {
  118. this.quadrilaterals = this._createQuadrilaterals(ignoreBorder);
  119. }
  120. }
  121. _createContainer(ignoreBorder = false) {
  122. const data = this.data,
  123. page = this.page,
  124. viewport = this.viewport;
  125. const container = document.createElement("section");
  126. let width = data.rect[2] - data.rect[0];
  127. let height = data.rect[3] - data.rect[1];
  128. container.setAttribute("data-annotation-id", data.id);
  129. const rect = _util.Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);
  130. if (data.hasOwnCanvas) {
  131. const transform = viewport.transform.slice();
  132. const [scaleX, scaleY] = _util.Util.singularValueDecompose2dScale(transform);
  133. width = Math.ceil(width * scaleX);
  134. height = Math.ceil(height * scaleY);
  135. rect[0] *= scaleX;
  136. rect[1] *= scaleY;
  137. for (let i = 0; i < 4; i++) {
  138. transform[i] = Math.sign(transform[i]);
  139. }
  140. container.style.transform = `matrix(${transform.join(",")})`;
  141. } else {
  142. container.style.transform = `matrix(${viewport.transform.join(",")})`;
  143. }
  144. container.style.transformOrigin = `${-rect[0]}px ${-rect[1]}px`;
  145. if (!ignoreBorder && data.borderStyle.width > 0) {
  146. container.style.borderWidth = `${data.borderStyle.width}px`;
  147. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  148. width -= 2 * data.borderStyle.width;
  149. height -= 2 * data.borderStyle.width;
  150. }
  151. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  152. const verticalRadius = data.borderStyle.verticalCornerRadius;
  153. if (horizontalRadius > 0 || verticalRadius > 0) {
  154. const radius = `${horizontalRadius}px / ${verticalRadius}px`;
  155. container.style.borderRadius = radius;
  156. }
  157. switch (data.borderStyle.style) {
  158. case _util.AnnotationBorderStyleType.SOLID:
  159. container.style.borderStyle = "solid";
  160. break;
  161. case _util.AnnotationBorderStyleType.DASHED:
  162. container.style.borderStyle = "dashed";
  163. break;
  164. case _util.AnnotationBorderStyleType.BEVELED:
  165. (0, _util.warn)("Unimplemented border style: beveled");
  166. break;
  167. case _util.AnnotationBorderStyleType.INSET:
  168. (0, _util.warn)("Unimplemented border style: inset");
  169. break;
  170. case _util.AnnotationBorderStyleType.UNDERLINE:
  171. container.style.borderBottomStyle = "solid";
  172. break;
  173. default:
  174. break;
  175. }
  176. const borderColor = data.borderColor || data.color || null;
  177. if (borderColor) {
  178. container.style.borderColor = _util.Util.makeHexColor(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  179. } else {
  180. container.style.borderWidth = 0;
  181. }
  182. }
  183. container.style.left = `${rect[0]}px`;
  184. container.style.top = `${rect[1]}px`;
  185. if (data.hasOwnCanvas) {
  186. container.style.width = container.style.height = "auto";
  187. } else {
  188. container.style.width = `${width}px`;
  189. container.style.height = `${height}px`;
  190. }
  191. return container;
  192. }
  193. _createQuadrilaterals(ignoreBorder = false) {
  194. if (!this.data.quadPoints) {
  195. return null;
  196. }
  197. const quadrilaterals = [];
  198. const savedRect = this.data.rect;
  199. for (const quadPoint of this.data.quadPoints) {
  200. this.data.rect = [quadPoint[2].x, quadPoint[2].y, quadPoint[1].x, quadPoint[1].y];
  201. quadrilaterals.push(this._createContainer(ignoreBorder));
  202. }
  203. this.data.rect = savedRect;
  204. return quadrilaterals;
  205. }
  206. _createPopup(trigger, data) {
  207. let container = this.container;
  208. if (this.quadrilaterals) {
  209. trigger = trigger || this.quadrilaterals;
  210. container = this.quadrilaterals[0];
  211. }
  212. if (!trigger) {
  213. trigger = document.createElement("div");
  214. trigger.style.height = container.style.height;
  215. trigger.style.width = container.style.width;
  216. container.appendChild(trigger);
  217. }
  218. const popupElement = new PopupElement({
  219. container,
  220. trigger,
  221. color: data.color,
  222. titleObj: data.titleObj,
  223. modificationDate: data.modificationDate,
  224. contentsObj: data.contentsObj,
  225. richText: data.richText,
  226. hideWrapper: true
  227. });
  228. const popup = popupElement.render();
  229. popup.style.left = container.style.width;
  230. container.appendChild(popup);
  231. }
  232. _renderQuadrilaterals(className) {
  233. for (const quadrilateral of this.quadrilaterals) {
  234. quadrilateral.className = className;
  235. }
  236. return this.quadrilaterals;
  237. }
  238. render() {
  239. (0, _util.unreachable)("Abstract method `AnnotationElement.render` called");
  240. }
  241. _getElementsByName(name, skipId = null) {
  242. const fields = [];
  243. if (this._fieldObjects) {
  244. const fieldObj = this._fieldObjects[name];
  245. if (fieldObj) {
  246. for (const {
  247. page,
  248. id,
  249. exportValues
  250. } of fieldObj) {
  251. if (page === -1) {
  252. continue;
  253. }
  254. if (id === skipId) {
  255. continue;
  256. }
  257. const exportValue = typeof exportValues === "string" ? exportValues : null;
  258. const domElement = document.getElementById(id);
  259. if (domElement && !GetElementsByNameSet.has(domElement)) {
  260. (0, _util.warn)(`_getElementsByName - element not allowed: ${id}`);
  261. continue;
  262. }
  263. fields.push({
  264. id,
  265. exportValue,
  266. domElement
  267. });
  268. }
  269. }
  270. return fields;
  271. }
  272. for (const domElement of document.getElementsByName(name)) {
  273. const {
  274. id,
  275. exportValue
  276. } = domElement;
  277. if (id === skipId) {
  278. continue;
  279. }
  280. if (!GetElementsByNameSet.has(domElement)) {
  281. continue;
  282. }
  283. fields.push({
  284. id,
  285. exportValue,
  286. domElement
  287. });
  288. }
  289. return fields;
  290. }
  291. static get platform() {
  292. const platform = typeof navigator !== "undefined" ? navigator.platform : "";
  293. return (0, _util.shadow)(this, "platform", {
  294. isWin: platform.includes("Win"),
  295. isMac: platform.includes("Mac")
  296. });
  297. }
  298. }
  299. class LinkAnnotationElement extends AnnotationElement {
  300. constructor(parameters, options = null) {
  301. const isRenderable = !!(parameters.data.url || parameters.data.dest || parameters.data.action || parameters.data.isTooltipOnly || parameters.data.resetForm || parameters.data.actions && (parameters.data.actions.Action || parameters.data.actions["Mouse Up"] || parameters.data.actions["Mouse Down"]));
  302. super(parameters, {
  303. isRenderable,
  304. ignoreBorder: !!options?.ignoreBorder,
  305. createQuadrilaterals: true
  306. });
  307. }
  308. render() {
  309. const {
  310. data,
  311. linkService
  312. } = this;
  313. const link = document.createElement("a");
  314. if (data.url) {
  315. if (!linkService.addLinkAttributes) {
  316. (0, _util.warn)("LinkAnnotationElement.render - missing `addLinkAttributes`-method on the `linkService`-instance.");
  317. }
  318. linkService.addLinkAttributes?.(link, data.url, data.newWindow);
  319. } else if (data.action) {
  320. this._bindNamedAction(link, data.action);
  321. } else if (data.dest) {
  322. this._bindLink(link, data.dest);
  323. } else {
  324. let hasClickAction = false;
  325. if (data.actions && (data.actions.Action || data.actions["Mouse Up"] || data.actions["Mouse Down"]) && this.enableScripting && this.hasJSActions) {
  326. hasClickAction = true;
  327. this._bindJSAction(link, data);
  328. }
  329. if (data.resetForm) {
  330. this._bindResetFormAction(link, data.resetForm);
  331. } else if (!hasClickAction) {
  332. this._bindLink(link, "");
  333. }
  334. }
  335. if (this.quadrilaterals) {
  336. return this._renderQuadrilaterals("linkAnnotation").map((quadrilateral, index) => {
  337. const linkElement = index === 0 ? link : link.cloneNode();
  338. quadrilateral.appendChild(linkElement);
  339. return quadrilateral;
  340. });
  341. }
  342. this.container.className = "linkAnnotation";
  343. this.container.appendChild(link);
  344. return this.container;
  345. }
  346. _bindLink(link, destination) {
  347. link.href = this.linkService.getDestinationHash(destination);
  348. link.onclick = () => {
  349. if (destination) {
  350. this.linkService.goToDestination(destination);
  351. }
  352. return false;
  353. };
  354. if (destination || destination === "") {
  355. link.className = "internalLink";
  356. }
  357. }
  358. _bindNamedAction(link, action) {
  359. link.href = this.linkService.getAnchorUrl("");
  360. link.onclick = () => {
  361. this.linkService.executeNamedAction(action);
  362. return false;
  363. };
  364. link.className = "internalLink";
  365. }
  366. _bindJSAction(link, data) {
  367. link.href = this.linkService.getAnchorUrl("");
  368. const map = new Map([["Action", "onclick"], ["Mouse Up", "onmouseup"], ["Mouse Down", "onmousedown"]]);
  369. for (const name of Object.keys(data.actions)) {
  370. const jsName = map.get(name);
  371. if (!jsName) {
  372. continue;
  373. }
  374. link[jsName] = () => {
  375. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  376. source: this,
  377. detail: {
  378. id: data.id,
  379. name
  380. }
  381. });
  382. return false;
  383. };
  384. }
  385. if (!link.onclick) {
  386. link.onclick = () => false;
  387. }
  388. link.className = "internalLink";
  389. }
  390. _bindResetFormAction(link, resetForm) {
  391. const otherClickAction = link.onclick;
  392. if (!otherClickAction) {
  393. link.href = this.linkService.getAnchorUrl("");
  394. }
  395. link.className = "internalLink";
  396. if (!this._fieldObjects) {
  397. (0, _util.warn)(`_bindResetFormAction - "resetForm" action not supported, ` + "ensure that the `fieldObjects` parameter is provided.");
  398. if (!otherClickAction) {
  399. link.onclick = () => false;
  400. }
  401. return;
  402. }
  403. link.onclick = () => {
  404. if (otherClickAction) {
  405. otherClickAction();
  406. }
  407. const {
  408. fields: resetFormFields,
  409. refs: resetFormRefs,
  410. include
  411. } = resetForm;
  412. const allFields = [];
  413. if (resetFormFields.length !== 0 || resetFormRefs.length !== 0) {
  414. const fieldIds = new Set(resetFormRefs);
  415. for (const fieldName of resetFormFields) {
  416. const fields = this._fieldObjects[fieldName] || [];
  417. for (const {
  418. id
  419. } of fields) {
  420. fieldIds.add(id);
  421. }
  422. }
  423. for (const fields of Object.values(this._fieldObjects)) {
  424. for (const field of fields) {
  425. if (fieldIds.has(field.id) === include) {
  426. allFields.push(field);
  427. }
  428. }
  429. }
  430. } else {
  431. for (const fields of Object.values(this._fieldObjects)) {
  432. allFields.push(...fields);
  433. }
  434. }
  435. const storage = this.annotationStorage;
  436. const allIds = [];
  437. for (const field of allFields) {
  438. const {
  439. id
  440. } = field;
  441. allIds.push(id);
  442. switch (field.type) {
  443. case "text":
  444. {
  445. const value = field.defaultValue || "";
  446. storage.setValue(id, {
  447. value,
  448. valueAsString: value
  449. });
  450. break;
  451. }
  452. case "checkbox":
  453. case "radiobutton":
  454. {
  455. const value = field.defaultValue === field.exportValues;
  456. storage.setValue(id, {
  457. value
  458. });
  459. break;
  460. }
  461. case "combobox":
  462. case "listbox":
  463. {
  464. const value = field.defaultValue || "";
  465. storage.setValue(id, {
  466. value
  467. });
  468. break;
  469. }
  470. default:
  471. continue;
  472. }
  473. const domElement = document.getElementById(id);
  474. if (!domElement || !GetElementsByNameSet.has(domElement)) {
  475. continue;
  476. }
  477. domElement.dispatchEvent(new Event("resetform"));
  478. }
  479. if (this.enableScripting) {
  480. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  481. source: this,
  482. detail: {
  483. id: "app",
  484. ids: allIds,
  485. name: "ResetForm"
  486. }
  487. });
  488. }
  489. return false;
  490. };
  491. }
  492. }
  493. class TextAnnotationElement extends AnnotationElement {
  494. constructor(parameters) {
  495. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  496. super(parameters, {
  497. isRenderable
  498. });
  499. }
  500. render() {
  501. this.container.className = "textAnnotation";
  502. const image = document.createElement("img");
  503. image.style.height = this.container.style.height;
  504. image.style.width = this.container.style.width;
  505. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  506. image.alt = "[{{type}} Annotation]";
  507. image.dataset.l10nId = "text_annotation_type";
  508. image.dataset.l10nArgs = JSON.stringify({
  509. type: this.data.name
  510. });
  511. if (!this.data.hasPopup) {
  512. this._createPopup(image, this.data);
  513. }
  514. this.container.appendChild(image);
  515. return this.container;
  516. }
  517. }
  518. class WidgetAnnotationElement extends AnnotationElement {
  519. render() {
  520. if (this.data.alternativeText) {
  521. this.container.title = this.data.alternativeText;
  522. }
  523. return this.container;
  524. }
  525. _getKeyModifier(event) {
  526. const {
  527. isWin,
  528. isMac
  529. } = AnnotationElement.platform;
  530. return isWin && event.ctrlKey || isMac && event.metaKey;
  531. }
  532. _setEventListener(element, baseName, eventName, valueGetter) {
  533. if (baseName.includes("mouse")) {
  534. element.addEventListener(baseName, event => {
  535. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  536. source: this,
  537. detail: {
  538. id: this.data.id,
  539. name: eventName,
  540. value: valueGetter(event),
  541. shift: event.shiftKey,
  542. modifier: this._getKeyModifier(event)
  543. }
  544. });
  545. });
  546. } else {
  547. element.addEventListener(baseName, event => {
  548. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  549. source: this,
  550. detail: {
  551. id: this.data.id,
  552. name: eventName,
  553. value: event.target.checked
  554. }
  555. });
  556. });
  557. }
  558. }
  559. _setEventListeners(element, names, getter) {
  560. for (const [baseName, eventName] of names) {
  561. if (eventName === "Action" || this.data.actions?.[eventName]) {
  562. this._setEventListener(element, baseName, eventName, getter);
  563. }
  564. }
  565. }
  566. _setBackgroundColor(element) {
  567. const color = this.data.backgroundColor || null;
  568. element.style.backgroundColor = color === null ? "transparent" : _util.Util.makeHexColor(color[0], color[1], color[2]);
  569. }
  570. _dispatchEventFromSandbox(actions, jsEvent) {
  571. const setColor = (jsName, styleName, event) => {
  572. const color = event.detail[jsName];
  573. event.target.style[styleName] = _scripting_utils.ColorConverters[`${color[0]}_HTML`](color.slice(1));
  574. };
  575. const commonActions = {
  576. display: event => {
  577. const hidden = event.detail.display % 2 === 1;
  578. event.target.style.visibility = hidden ? "hidden" : "visible";
  579. this.annotationStorage.setValue(this.data.id, {
  580. hidden,
  581. print: event.detail.display === 0 || event.detail.display === 3
  582. });
  583. },
  584. print: event => {
  585. this.annotationStorage.setValue(this.data.id, {
  586. print: event.detail.print
  587. });
  588. },
  589. hidden: event => {
  590. event.target.style.visibility = event.detail.hidden ? "hidden" : "visible";
  591. this.annotationStorage.setValue(this.data.id, {
  592. hidden: event.detail.hidden
  593. });
  594. },
  595. focus: event => {
  596. setTimeout(() => event.target.focus({
  597. preventScroll: false
  598. }), 0);
  599. },
  600. userName: event => {
  601. event.target.title = event.detail.userName;
  602. },
  603. readonly: event => {
  604. if (event.detail.readonly) {
  605. event.target.setAttribute("readonly", "");
  606. } else {
  607. event.target.removeAttribute("readonly");
  608. }
  609. },
  610. required: event => {
  611. if (event.detail.required) {
  612. event.target.setAttribute("required", "");
  613. } else {
  614. event.target.removeAttribute("required");
  615. }
  616. },
  617. bgColor: event => {
  618. setColor("bgColor", "backgroundColor", event);
  619. },
  620. fillColor: event => {
  621. setColor("fillColor", "backgroundColor", event);
  622. },
  623. fgColor: event => {
  624. setColor("fgColor", "color", event);
  625. },
  626. textColor: event => {
  627. setColor("textColor", "color", event);
  628. },
  629. borderColor: event => {
  630. setColor("borderColor", "borderColor", event);
  631. },
  632. strokeColor: event => {
  633. setColor("strokeColor", "borderColor", event);
  634. }
  635. };
  636. for (const name of Object.keys(jsEvent.detail)) {
  637. const action = actions[name] || commonActions[name];
  638. if (action) {
  639. action(jsEvent);
  640. }
  641. }
  642. }
  643. }
  644. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  645. constructor(parameters) {
  646. const isRenderable = parameters.renderForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  647. super(parameters, {
  648. isRenderable
  649. });
  650. }
  651. setPropertyOnSiblings(base, key, value, keyInStorage) {
  652. const storage = this.annotationStorage;
  653. for (const element of this._getElementsByName(base.name, base.id)) {
  654. if (element.domElement) {
  655. element.domElement[key] = value;
  656. }
  657. storage.setValue(element.id, {
  658. [keyInStorage]: value
  659. });
  660. }
  661. }
  662. render() {
  663. const storage = this.annotationStorage;
  664. const id = this.data.id;
  665. this.container.className = "textWidgetAnnotation";
  666. let element = null;
  667. if (this.renderForms) {
  668. const storedData = storage.getValue(id, {
  669. value: this.data.fieldValue,
  670. valueAsString: this.data.fieldValue
  671. });
  672. const textContent = storedData.valueAsString || storedData.value || "";
  673. const elementData = {
  674. userValue: null,
  675. formattedValue: null,
  676. beforeInputSelectionRange: null,
  677. beforeInputValue: null
  678. };
  679. if (this.data.multiLine) {
  680. element = document.createElement("textarea");
  681. element.textContent = textContent;
  682. } else {
  683. element = document.createElement("input");
  684. element.type = "text";
  685. element.setAttribute("value", textContent);
  686. }
  687. GetElementsByNameSet.add(element);
  688. element.disabled = this.data.readOnly;
  689. element.name = this.data.fieldName;
  690. element.tabIndex = DEFAULT_TAB_INDEX;
  691. elementData.userValue = textContent;
  692. element.setAttribute("id", id);
  693. element.addEventListener("input", event => {
  694. storage.setValue(id, {
  695. value: event.target.value
  696. });
  697. this.setPropertyOnSiblings(element, "value", event.target.value, "value");
  698. });
  699. element.addEventListener("resetform", event => {
  700. const defaultValue = this.data.defaultFieldValue || "";
  701. element.value = elementData.userValue = defaultValue;
  702. delete elementData.formattedValue;
  703. });
  704. let blurListener = event => {
  705. if (elementData.formattedValue) {
  706. event.target.value = elementData.formattedValue;
  707. }
  708. event.target.scrollLeft = 0;
  709. elementData.beforeInputSelectionRange = null;
  710. };
  711. if (this.enableScripting && this.hasJSActions) {
  712. element.addEventListener("focus", event => {
  713. if (elementData.userValue) {
  714. event.target.value = elementData.userValue;
  715. }
  716. });
  717. element.addEventListener("updatefromsandbox", jsEvent => {
  718. const actions = {
  719. value(event) {
  720. elementData.userValue = event.detail.value || "";
  721. storage.setValue(id, {
  722. value: elementData.userValue.toString()
  723. });
  724. if (!elementData.formattedValue) {
  725. event.target.value = elementData.userValue;
  726. }
  727. },
  728. valueAsString(event) {
  729. elementData.formattedValue = event.detail.valueAsString || "";
  730. if (event.target !== document.activeElement) {
  731. event.target.value = elementData.formattedValue;
  732. }
  733. storage.setValue(id, {
  734. formattedValue: elementData.formattedValue
  735. });
  736. },
  737. selRange(event) {
  738. const [selStart, selEnd] = event.detail.selRange;
  739. if (selStart >= 0 && selEnd < event.target.value.length) {
  740. event.target.setSelectionRange(selStart, selEnd);
  741. }
  742. }
  743. };
  744. this._dispatchEventFromSandbox(actions, jsEvent);
  745. });
  746. element.addEventListener("keydown", event => {
  747. elementData.beforeInputValue = event.target.value;
  748. let commitKey = -1;
  749. if (event.key === "Escape") {
  750. commitKey = 0;
  751. } else if (event.key === "Enter") {
  752. commitKey = 2;
  753. } else if (event.key === "Tab") {
  754. commitKey = 3;
  755. }
  756. if (commitKey === -1) {
  757. return;
  758. }
  759. elementData.userValue = event.target.value;
  760. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  761. source: this,
  762. detail: {
  763. id,
  764. name: "Keystroke",
  765. value: event.target.value,
  766. willCommit: true,
  767. commitKey,
  768. selStart: event.target.selectionStart,
  769. selEnd: event.target.selectionEnd
  770. }
  771. });
  772. });
  773. const _blurListener = blurListener;
  774. blurListener = null;
  775. element.addEventListener("blur", event => {
  776. if (this._mouseState.isDown) {
  777. elementData.userValue = event.target.value;
  778. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  779. source: this,
  780. detail: {
  781. id,
  782. name: "Keystroke",
  783. value: event.target.value,
  784. willCommit: true,
  785. commitKey: 1,
  786. selStart: event.target.selectionStart,
  787. selEnd: event.target.selectionEnd
  788. }
  789. });
  790. }
  791. _blurListener(event);
  792. });
  793. element.addEventListener("mousedown", event => {
  794. elementData.beforeInputValue = event.target.value;
  795. elementData.beforeInputSelectionRange = null;
  796. });
  797. element.addEventListener("keyup", event => {
  798. if (event.target.selectionStart === event.target.selectionEnd) {
  799. elementData.beforeInputSelectionRange = null;
  800. }
  801. });
  802. element.addEventListener("select", event => {
  803. elementData.beforeInputSelectionRange = [event.target.selectionStart, event.target.selectionEnd];
  804. });
  805. if (this.data.actions?.Keystroke) {
  806. element.addEventListener("input", event => {
  807. let selStart = -1;
  808. let selEnd = -1;
  809. if (elementData.beforeInputSelectionRange) {
  810. [selStart, selEnd] = elementData.beforeInputSelectionRange;
  811. }
  812. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  813. source: this,
  814. detail: {
  815. id,
  816. name: "Keystroke",
  817. value: elementData.beforeInputValue,
  818. change: event.data,
  819. willCommit: false,
  820. selStart,
  821. selEnd
  822. }
  823. });
  824. });
  825. }
  826. this._setEventListeners(element, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  827. }
  828. if (blurListener) {
  829. element.addEventListener("blur", blurListener);
  830. }
  831. if (this.data.maxLen !== null) {
  832. element.maxLength = this.data.maxLen;
  833. }
  834. if (this.data.comb) {
  835. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  836. const combWidth = fieldWidth / this.data.maxLen;
  837. element.classList.add("comb");
  838. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  839. }
  840. } else {
  841. element = document.createElement("div");
  842. element.textContent = this.data.fieldValue;
  843. element.style.verticalAlign = "middle";
  844. element.style.display = "table-cell";
  845. }
  846. this._setTextStyle(element);
  847. this._setBackgroundColor(element);
  848. this.container.appendChild(element);
  849. return this.container;
  850. }
  851. _setTextStyle(element) {
  852. const TEXT_ALIGNMENT = ["left", "center", "right"];
  853. const {
  854. fontSize,
  855. fontColor
  856. } = this.data.defaultAppearanceData;
  857. const style = element.style;
  858. if (fontSize) {
  859. style.fontSize = `${fontSize}px`;
  860. }
  861. style.color = _util.Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  862. if (this.data.textAlignment !== null) {
  863. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  864. }
  865. }
  866. }
  867. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  868. constructor(parameters) {
  869. super(parameters, {
  870. isRenderable: parameters.renderForms
  871. });
  872. }
  873. render() {
  874. const storage = this.annotationStorage;
  875. const data = this.data;
  876. const id = data.id;
  877. let value = storage.getValue(id, {
  878. value: data.exportValue === data.fieldValue
  879. }).value;
  880. if (typeof value === "string") {
  881. value = value !== "Off";
  882. storage.setValue(id, {
  883. value
  884. });
  885. }
  886. this.container.className = "buttonWidgetAnnotation checkBox";
  887. const element = document.createElement("input");
  888. GetElementsByNameSet.add(element);
  889. element.disabled = data.readOnly;
  890. element.type = "checkbox";
  891. element.name = data.fieldName;
  892. if (value) {
  893. element.setAttribute("checked", true);
  894. }
  895. element.setAttribute("id", id);
  896. element.setAttribute("exportValue", data.exportValue);
  897. element.tabIndex = DEFAULT_TAB_INDEX;
  898. element.addEventListener("change", event => {
  899. const {
  900. name,
  901. checked
  902. } = event.target;
  903. for (const checkbox of this._getElementsByName(name, id)) {
  904. const curChecked = checked && checkbox.exportValue === data.exportValue;
  905. if (checkbox.domElement) {
  906. checkbox.domElement.checked = curChecked;
  907. }
  908. storage.setValue(checkbox.id, {
  909. value: curChecked
  910. });
  911. }
  912. storage.setValue(id, {
  913. value: checked
  914. });
  915. });
  916. element.addEventListener("resetform", event => {
  917. const defaultValue = data.defaultFieldValue || "Off";
  918. event.target.checked = defaultValue === data.exportValue;
  919. });
  920. if (this.enableScripting && this.hasJSActions) {
  921. element.addEventListener("updatefromsandbox", jsEvent => {
  922. const actions = {
  923. value(event) {
  924. event.target.checked = event.detail.value !== "Off";
  925. storage.setValue(id, {
  926. value: event.target.checked
  927. });
  928. }
  929. };
  930. this._dispatchEventFromSandbox(actions, jsEvent);
  931. });
  932. this._setEventListeners(element, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  933. }
  934. this._setBackgroundColor(element);
  935. this.container.appendChild(element);
  936. return this.container;
  937. }
  938. }
  939. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  940. constructor(parameters) {
  941. super(parameters, {
  942. isRenderable: parameters.renderForms
  943. });
  944. }
  945. render() {
  946. this.container.className = "buttonWidgetAnnotation radioButton";
  947. const storage = this.annotationStorage;
  948. const data = this.data;
  949. const id = data.id;
  950. let value = storage.getValue(id, {
  951. value: data.fieldValue === data.buttonValue
  952. }).value;
  953. if (typeof value === "string") {
  954. value = value !== data.buttonValue;
  955. storage.setValue(id, {
  956. value
  957. });
  958. }
  959. const element = document.createElement("input");
  960. GetElementsByNameSet.add(element);
  961. element.disabled = data.readOnly;
  962. element.type = "radio";
  963. element.name = data.fieldName;
  964. if (value) {
  965. element.setAttribute("checked", true);
  966. }
  967. element.setAttribute("id", id);
  968. element.tabIndex = DEFAULT_TAB_INDEX;
  969. element.addEventListener("change", event => {
  970. const {
  971. name,
  972. checked
  973. } = event.target;
  974. for (const radio of this._getElementsByName(name, id)) {
  975. storage.setValue(radio.id, {
  976. value: false
  977. });
  978. }
  979. storage.setValue(id, {
  980. value: checked
  981. });
  982. });
  983. element.addEventListener("resetform", event => {
  984. const defaultValue = data.defaultFieldValue;
  985. event.target.checked = defaultValue !== null && defaultValue !== undefined && defaultValue === data.buttonValue;
  986. });
  987. if (this.enableScripting && this.hasJSActions) {
  988. const pdfButtonValue = data.buttonValue;
  989. element.addEventListener("updatefromsandbox", jsEvent => {
  990. const actions = {
  991. value: event => {
  992. const checked = pdfButtonValue === event.detail.value;
  993. for (const radio of this._getElementsByName(event.target.name)) {
  994. const curChecked = checked && radio.id === id;
  995. if (radio.domElement) {
  996. radio.domElement.checked = curChecked;
  997. }
  998. storage.setValue(radio.id, {
  999. value: curChecked
  1000. });
  1001. }
  1002. }
  1003. };
  1004. this._dispatchEventFromSandbox(actions, jsEvent);
  1005. });
  1006. this._setEventListeners(element, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  1007. }
  1008. this._setBackgroundColor(element);
  1009. this.container.appendChild(element);
  1010. return this.container;
  1011. }
  1012. }
  1013. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  1014. constructor(parameters) {
  1015. super(parameters, {
  1016. ignoreBorder: parameters.data.hasAppearance
  1017. });
  1018. }
  1019. render() {
  1020. const container = super.render();
  1021. container.className = "buttonWidgetAnnotation pushButton";
  1022. if (this.data.alternativeText) {
  1023. container.title = this.data.alternativeText;
  1024. }
  1025. return container;
  1026. }
  1027. }
  1028. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  1029. constructor(parameters) {
  1030. super(parameters, {
  1031. isRenderable: parameters.renderForms
  1032. });
  1033. }
  1034. render() {
  1035. this.container.className = "choiceWidgetAnnotation";
  1036. const storage = this.annotationStorage;
  1037. const id = this.data.id;
  1038. storage.getValue(id, {
  1039. value: this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : undefined
  1040. });
  1041. let {
  1042. fontSize
  1043. } = this.data.defaultAppearanceData;
  1044. if (!fontSize) {
  1045. fontSize = 9;
  1046. }
  1047. const fontSizeStyle = `calc(${fontSize}px * var(--zoom-factor))`;
  1048. const selectElement = document.createElement("select");
  1049. GetElementsByNameSet.add(selectElement);
  1050. selectElement.disabled = this.data.readOnly;
  1051. selectElement.name = this.data.fieldName;
  1052. selectElement.setAttribute("id", id);
  1053. selectElement.tabIndex = DEFAULT_TAB_INDEX;
  1054. selectElement.style.fontSize = `${fontSize}px`;
  1055. if (!this.data.combo) {
  1056. selectElement.size = this.data.options.length;
  1057. if (this.data.multiSelect) {
  1058. selectElement.multiple = true;
  1059. }
  1060. }
  1061. selectElement.addEventListener("resetform", event => {
  1062. const defaultValue = this.data.defaultFieldValue;
  1063. for (const option of selectElement.options) {
  1064. option.selected = option.value === defaultValue;
  1065. }
  1066. });
  1067. for (const option of this.data.options) {
  1068. const optionElement = document.createElement("option");
  1069. optionElement.textContent = option.displayValue;
  1070. optionElement.value = option.exportValue;
  1071. if (this.data.combo) {
  1072. optionElement.style.fontSize = fontSizeStyle;
  1073. }
  1074. if (this.data.fieldValue.includes(option.exportValue)) {
  1075. optionElement.setAttribute("selected", true);
  1076. }
  1077. selectElement.appendChild(optionElement);
  1078. }
  1079. const getValue = (event, isExport) => {
  1080. const name = isExport ? "value" : "textContent";
  1081. const options = event.target.options;
  1082. if (!event.target.multiple) {
  1083. return options.selectedIndex === -1 ? null : options[options.selectedIndex][name];
  1084. }
  1085. return Array.prototype.filter.call(options, option => option.selected).map(option => option[name]);
  1086. };
  1087. const getItems = event => {
  1088. const options = event.target.options;
  1089. return Array.prototype.map.call(options, option => {
  1090. return {
  1091. displayValue: option.textContent,
  1092. exportValue: option.value
  1093. };
  1094. });
  1095. };
  1096. if (this.enableScripting && this.hasJSActions) {
  1097. selectElement.addEventListener("updatefromsandbox", jsEvent => {
  1098. const actions = {
  1099. value(event) {
  1100. const value = event.detail.value;
  1101. const values = new Set(Array.isArray(value) ? value : [value]);
  1102. for (const option of selectElement.options) {
  1103. option.selected = values.has(option.value);
  1104. }
  1105. storage.setValue(id, {
  1106. value: getValue(event, true)
  1107. });
  1108. },
  1109. multipleSelection(event) {
  1110. selectElement.multiple = true;
  1111. },
  1112. remove(event) {
  1113. const options = selectElement.options;
  1114. const index = event.detail.remove;
  1115. options[index].selected = false;
  1116. selectElement.remove(index);
  1117. if (options.length > 0) {
  1118. const i = Array.prototype.findIndex.call(options, option => option.selected);
  1119. if (i === -1) {
  1120. options[0].selected = true;
  1121. }
  1122. }
  1123. storage.setValue(id, {
  1124. value: getValue(event, true),
  1125. items: getItems(event)
  1126. });
  1127. },
  1128. clear(event) {
  1129. while (selectElement.length !== 0) {
  1130. selectElement.remove(0);
  1131. }
  1132. storage.setValue(id, {
  1133. value: null,
  1134. items: []
  1135. });
  1136. },
  1137. insert(event) {
  1138. const {
  1139. index,
  1140. displayValue,
  1141. exportValue
  1142. } = event.detail.insert;
  1143. const optionElement = document.createElement("option");
  1144. optionElement.textContent = displayValue;
  1145. optionElement.value = exportValue;
  1146. selectElement.insertBefore(optionElement, selectElement.children[index]);
  1147. storage.setValue(id, {
  1148. value: getValue(event, true),
  1149. items: getItems(event)
  1150. });
  1151. },
  1152. items(event) {
  1153. const {
  1154. items
  1155. } = event.detail;
  1156. while (selectElement.length !== 0) {
  1157. selectElement.remove(0);
  1158. }
  1159. for (const item of items) {
  1160. const {
  1161. displayValue,
  1162. exportValue
  1163. } = item;
  1164. const optionElement = document.createElement("option");
  1165. optionElement.textContent = displayValue;
  1166. optionElement.value = exportValue;
  1167. selectElement.appendChild(optionElement);
  1168. }
  1169. if (selectElement.options.length > 0) {
  1170. selectElement.options[0].selected = true;
  1171. }
  1172. storage.setValue(id, {
  1173. value: getValue(event, true),
  1174. items: getItems(event)
  1175. });
  1176. },
  1177. indices(event) {
  1178. const indices = new Set(event.detail.indices);
  1179. for (const option of event.target.options) {
  1180. option.selected = indices.has(option.index);
  1181. }
  1182. storage.setValue(id, {
  1183. value: getValue(event, true)
  1184. });
  1185. },
  1186. editable(event) {
  1187. event.target.disabled = !event.detail.editable;
  1188. }
  1189. };
  1190. this._dispatchEventFromSandbox(actions, jsEvent);
  1191. });
  1192. selectElement.addEventListener("input", event => {
  1193. const exportValue = getValue(event, true);
  1194. const value = getValue(event, false);
  1195. storage.setValue(id, {
  1196. value: exportValue
  1197. });
  1198. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  1199. source: this,
  1200. detail: {
  1201. id,
  1202. name: "Keystroke",
  1203. value,
  1204. changeEx: exportValue,
  1205. willCommit: true,
  1206. commitKey: 1,
  1207. keyDown: false
  1208. }
  1209. });
  1210. });
  1211. this._setEventListeners(selectElement, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"], ["input", "Action"]], event => event.target.checked);
  1212. } else {
  1213. selectElement.addEventListener("input", function (event) {
  1214. storage.setValue(id, {
  1215. value: getValue(event)
  1216. });
  1217. });
  1218. }
  1219. this._setBackgroundColor(selectElement);
  1220. this.container.appendChild(selectElement);
  1221. return this.container;
  1222. }
  1223. }
  1224. class PopupAnnotationElement extends AnnotationElement {
  1225. constructor(parameters) {
  1226. const isRenderable = !!(parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1227. super(parameters, {
  1228. isRenderable
  1229. });
  1230. }
  1231. render() {
  1232. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  1233. this.container.className = "popupAnnotation";
  1234. if (IGNORE_TYPES.includes(this.data.parentType)) {
  1235. return this.container;
  1236. }
  1237. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  1238. const parentElements = this.layer.querySelectorAll(selector);
  1239. if (parentElements.length === 0) {
  1240. return this.container;
  1241. }
  1242. const popup = new PopupElement({
  1243. container: this.container,
  1244. trigger: Array.from(parentElements),
  1245. color: this.data.color,
  1246. titleObj: this.data.titleObj,
  1247. modificationDate: this.data.modificationDate,
  1248. contentsObj: this.data.contentsObj,
  1249. richText: this.data.richText
  1250. });
  1251. const page = this.page;
  1252. const rect = _util.Util.normalizeRect([this.data.parentRect[0], page.view[3] - this.data.parentRect[1] + page.view[1], this.data.parentRect[2], page.view[3] - this.data.parentRect[3] + page.view[1]]);
  1253. const popupLeft = rect[0] + this.data.parentRect[2] - this.data.parentRect[0];
  1254. const popupTop = rect[1];
  1255. this.container.style.transformOrigin = `${-popupLeft}px ${-popupTop}px`;
  1256. this.container.style.left = `${popupLeft}px`;
  1257. this.container.style.top = `${popupTop}px`;
  1258. this.container.appendChild(popup.render());
  1259. return this.container;
  1260. }
  1261. }
  1262. class PopupElement {
  1263. constructor(parameters) {
  1264. this.container = parameters.container;
  1265. this.trigger = parameters.trigger;
  1266. this.color = parameters.color;
  1267. this.titleObj = parameters.titleObj;
  1268. this.modificationDate = parameters.modificationDate;
  1269. this.contentsObj = parameters.contentsObj;
  1270. this.richText = parameters.richText;
  1271. this.hideWrapper = parameters.hideWrapper || false;
  1272. this.pinned = false;
  1273. }
  1274. render() {
  1275. const BACKGROUND_ENLIGHT = 0.7;
  1276. const wrapper = document.createElement("div");
  1277. wrapper.className = "popupWrapper";
  1278. this.hideElement = this.hideWrapper ? wrapper : this.container;
  1279. this.hideElement.hidden = true;
  1280. const popup = document.createElement("div");
  1281. popup.className = "popup";
  1282. const color = this.color;
  1283. if (color) {
  1284. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  1285. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  1286. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  1287. popup.style.backgroundColor = _util.Util.makeHexColor(r | 0, g | 0, b | 0);
  1288. }
  1289. const title = document.createElement("h1");
  1290. title.dir = this.titleObj.dir;
  1291. title.textContent = this.titleObj.str;
  1292. popup.appendChild(title);
  1293. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  1294. if (dateObject) {
  1295. const modificationDate = document.createElement("span");
  1296. modificationDate.className = "popupDate";
  1297. modificationDate.textContent = "{{date}}, {{time}}";
  1298. modificationDate.dataset.l10nId = "annotation_date_string";
  1299. modificationDate.dataset.l10nArgs = JSON.stringify({
  1300. date: dateObject.toLocaleDateString(),
  1301. time: dateObject.toLocaleTimeString()
  1302. });
  1303. popup.appendChild(modificationDate);
  1304. }
  1305. if (this.richText?.str && (!this.contentsObj?.str || this.contentsObj.str === this.richText.str)) {
  1306. _xfa_layer.XfaLayer.render({
  1307. xfaHtml: this.richText.html,
  1308. intent: "richText",
  1309. div: popup
  1310. });
  1311. popup.lastChild.className = "richText popupContent";
  1312. } else {
  1313. const contents = this._formatContents(this.contentsObj);
  1314. popup.appendChild(contents);
  1315. }
  1316. if (!Array.isArray(this.trigger)) {
  1317. this.trigger = [this.trigger];
  1318. }
  1319. for (const element of this.trigger) {
  1320. element.addEventListener("click", this._toggle.bind(this));
  1321. element.addEventListener("mouseover", this._show.bind(this, false));
  1322. element.addEventListener("mouseout", this._hide.bind(this, false));
  1323. }
  1324. popup.addEventListener("click", this._hide.bind(this, true));
  1325. wrapper.appendChild(popup);
  1326. return wrapper;
  1327. }
  1328. _formatContents({
  1329. str,
  1330. dir
  1331. }) {
  1332. const p = document.createElement("p");
  1333. p.className = "popupContent";
  1334. p.dir = dir;
  1335. const lines = str.split(/(?:\r\n?|\n)/);
  1336. for (let i = 0, ii = lines.length; i < ii; ++i) {
  1337. const line = lines[i];
  1338. p.appendChild(document.createTextNode(line));
  1339. if (i < ii - 1) {
  1340. p.appendChild(document.createElement("br"));
  1341. }
  1342. }
  1343. return p;
  1344. }
  1345. _toggle() {
  1346. if (this.pinned) {
  1347. this._hide(true);
  1348. } else {
  1349. this._show(true);
  1350. }
  1351. }
  1352. _show(pin = false) {
  1353. if (pin) {
  1354. this.pinned = true;
  1355. }
  1356. if (this.hideElement.hidden) {
  1357. this.hideElement.hidden = false;
  1358. this.container.style.zIndex += 1;
  1359. }
  1360. }
  1361. _hide(unpin = true) {
  1362. if (unpin) {
  1363. this.pinned = false;
  1364. }
  1365. if (!this.hideElement.hidden && !this.pinned) {
  1366. this.hideElement.hidden = true;
  1367. this.container.style.zIndex -= 1;
  1368. }
  1369. }
  1370. }
  1371. class FreeTextAnnotationElement extends AnnotationElement {
  1372. constructor(parameters) {
  1373. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1374. super(parameters, {
  1375. isRenderable,
  1376. ignoreBorder: true
  1377. });
  1378. }
  1379. render() {
  1380. this.container.className = "freeTextAnnotation";
  1381. if (!this.data.hasPopup) {
  1382. this._createPopup(null, this.data);
  1383. }
  1384. return this.container;
  1385. }
  1386. }
  1387. class LineAnnotationElement extends AnnotationElement {
  1388. constructor(parameters) {
  1389. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1390. super(parameters, {
  1391. isRenderable,
  1392. ignoreBorder: true
  1393. });
  1394. }
  1395. render() {
  1396. this.container.className = "lineAnnotation";
  1397. const data = this.data;
  1398. const width = data.rect[2] - data.rect[0];
  1399. const height = data.rect[3] - data.rect[1];
  1400. const svg = this.svgFactory.create(width, height);
  1401. const line = this.svgFactory.createElement("svg:line");
  1402. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  1403. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  1404. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  1405. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  1406. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  1407. line.setAttribute("stroke", "transparent");
  1408. line.setAttribute("fill", "transparent");
  1409. svg.appendChild(line);
  1410. this.container.append(svg);
  1411. this._createPopup(line, data);
  1412. return this.container;
  1413. }
  1414. }
  1415. class SquareAnnotationElement extends AnnotationElement {
  1416. constructor(parameters) {
  1417. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1418. super(parameters, {
  1419. isRenderable,
  1420. ignoreBorder: true
  1421. });
  1422. }
  1423. render() {
  1424. this.container.className = "squareAnnotation";
  1425. const data = this.data;
  1426. const width = data.rect[2] - data.rect[0];
  1427. const height = data.rect[3] - data.rect[1];
  1428. const svg = this.svgFactory.create(width, height);
  1429. const borderWidth = data.borderStyle.width;
  1430. const square = this.svgFactory.createElement("svg:rect");
  1431. square.setAttribute("x", borderWidth / 2);
  1432. square.setAttribute("y", borderWidth / 2);
  1433. square.setAttribute("width", width - borderWidth);
  1434. square.setAttribute("height", height - borderWidth);
  1435. square.setAttribute("stroke-width", borderWidth || 1);
  1436. square.setAttribute("stroke", "transparent");
  1437. square.setAttribute("fill", "transparent");
  1438. svg.appendChild(square);
  1439. this.container.append(svg);
  1440. this._createPopup(square, data);
  1441. return this.container;
  1442. }
  1443. }
  1444. class CircleAnnotationElement extends AnnotationElement {
  1445. constructor(parameters) {
  1446. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1447. super(parameters, {
  1448. isRenderable,
  1449. ignoreBorder: true
  1450. });
  1451. }
  1452. render() {
  1453. this.container.className = "circleAnnotation";
  1454. const data = this.data;
  1455. const width = data.rect[2] - data.rect[0];
  1456. const height = data.rect[3] - data.rect[1];
  1457. const svg = this.svgFactory.create(width, height);
  1458. const borderWidth = data.borderStyle.width;
  1459. const circle = this.svgFactory.createElement("svg:ellipse");
  1460. circle.setAttribute("cx", width / 2);
  1461. circle.setAttribute("cy", height / 2);
  1462. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  1463. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  1464. circle.setAttribute("stroke-width", borderWidth || 1);
  1465. circle.setAttribute("stroke", "transparent");
  1466. circle.setAttribute("fill", "transparent");
  1467. svg.appendChild(circle);
  1468. this.container.append(svg);
  1469. this._createPopup(circle, data);
  1470. return this.container;
  1471. }
  1472. }
  1473. class PolylineAnnotationElement extends AnnotationElement {
  1474. constructor(parameters) {
  1475. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1476. super(parameters, {
  1477. isRenderable,
  1478. ignoreBorder: true
  1479. });
  1480. this.containerClassName = "polylineAnnotation";
  1481. this.svgElementName = "svg:polyline";
  1482. }
  1483. render() {
  1484. this.container.className = this.containerClassName;
  1485. const data = this.data;
  1486. const width = data.rect[2] - data.rect[0];
  1487. const height = data.rect[3] - data.rect[1];
  1488. const svg = this.svgFactory.create(width, height);
  1489. let points = [];
  1490. for (const coordinate of data.vertices) {
  1491. const x = coordinate.x - data.rect[0];
  1492. const y = data.rect[3] - coordinate.y;
  1493. points.push(x + "," + y);
  1494. }
  1495. points = points.join(" ");
  1496. const polyline = this.svgFactory.createElement(this.svgElementName);
  1497. polyline.setAttribute("points", points);
  1498. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1499. polyline.setAttribute("stroke", "transparent");
  1500. polyline.setAttribute("fill", "transparent");
  1501. svg.appendChild(polyline);
  1502. this.container.append(svg);
  1503. this._createPopup(polyline, data);
  1504. return this.container;
  1505. }
  1506. }
  1507. class PolygonAnnotationElement extends PolylineAnnotationElement {
  1508. constructor(parameters) {
  1509. super(parameters);
  1510. this.containerClassName = "polygonAnnotation";
  1511. this.svgElementName = "svg:polygon";
  1512. }
  1513. }
  1514. class CaretAnnotationElement extends AnnotationElement {
  1515. constructor(parameters) {
  1516. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1517. super(parameters, {
  1518. isRenderable,
  1519. ignoreBorder: true
  1520. });
  1521. }
  1522. render() {
  1523. this.container.className = "caretAnnotation";
  1524. if (!this.data.hasPopup) {
  1525. this._createPopup(null, this.data);
  1526. }
  1527. return this.container;
  1528. }
  1529. }
  1530. class InkAnnotationElement extends AnnotationElement {
  1531. constructor(parameters) {
  1532. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1533. super(parameters, {
  1534. isRenderable,
  1535. ignoreBorder: true
  1536. });
  1537. this.containerClassName = "inkAnnotation";
  1538. this.svgElementName = "svg:polyline";
  1539. }
  1540. render() {
  1541. this.container.className = this.containerClassName;
  1542. const data = this.data;
  1543. const width = data.rect[2] - data.rect[0];
  1544. const height = data.rect[3] - data.rect[1];
  1545. const svg = this.svgFactory.create(width, height);
  1546. for (const inkList of data.inkLists) {
  1547. let points = [];
  1548. for (const coordinate of inkList) {
  1549. const x = coordinate.x - data.rect[0];
  1550. const y = data.rect[3] - coordinate.y;
  1551. points.push(`${x},${y}`);
  1552. }
  1553. points = points.join(" ");
  1554. const polyline = this.svgFactory.createElement(this.svgElementName);
  1555. polyline.setAttribute("points", points);
  1556. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1557. polyline.setAttribute("stroke", "transparent");
  1558. polyline.setAttribute("fill", "transparent");
  1559. this._createPopup(polyline, data);
  1560. svg.appendChild(polyline);
  1561. }
  1562. this.container.append(svg);
  1563. return this.container;
  1564. }
  1565. }
  1566. class HighlightAnnotationElement extends AnnotationElement {
  1567. constructor(parameters) {
  1568. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1569. super(parameters, {
  1570. isRenderable,
  1571. ignoreBorder: true,
  1572. createQuadrilaterals: true
  1573. });
  1574. }
  1575. render() {
  1576. if (!this.data.hasPopup) {
  1577. this._createPopup(null, this.data);
  1578. }
  1579. if (this.quadrilaterals) {
  1580. return this._renderQuadrilaterals("highlightAnnotation");
  1581. }
  1582. this.container.className = "highlightAnnotation";
  1583. return this.container;
  1584. }
  1585. }
  1586. class UnderlineAnnotationElement extends AnnotationElement {
  1587. constructor(parameters) {
  1588. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1589. super(parameters, {
  1590. isRenderable,
  1591. ignoreBorder: true,
  1592. createQuadrilaterals: true
  1593. });
  1594. }
  1595. render() {
  1596. if (!this.data.hasPopup) {
  1597. this._createPopup(null, this.data);
  1598. }
  1599. if (this.quadrilaterals) {
  1600. return this._renderQuadrilaterals("underlineAnnotation");
  1601. }
  1602. this.container.className = "underlineAnnotation";
  1603. return this.container;
  1604. }
  1605. }
  1606. class SquigglyAnnotationElement extends AnnotationElement {
  1607. constructor(parameters) {
  1608. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1609. super(parameters, {
  1610. isRenderable,
  1611. ignoreBorder: true,
  1612. createQuadrilaterals: true
  1613. });
  1614. }
  1615. render() {
  1616. if (!this.data.hasPopup) {
  1617. this._createPopup(null, this.data);
  1618. }
  1619. if (this.quadrilaterals) {
  1620. return this._renderQuadrilaterals("squigglyAnnotation");
  1621. }
  1622. this.container.className = "squigglyAnnotation";
  1623. return this.container;
  1624. }
  1625. }
  1626. class StrikeOutAnnotationElement extends AnnotationElement {
  1627. constructor(parameters) {
  1628. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1629. super(parameters, {
  1630. isRenderable,
  1631. ignoreBorder: true,
  1632. createQuadrilaterals: true
  1633. });
  1634. }
  1635. render() {
  1636. if (!this.data.hasPopup) {
  1637. this._createPopup(null, this.data);
  1638. }
  1639. if (this.quadrilaterals) {
  1640. return this._renderQuadrilaterals("strikeoutAnnotation");
  1641. }
  1642. this.container.className = "strikeoutAnnotation";
  1643. return this.container;
  1644. }
  1645. }
  1646. class StampAnnotationElement extends AnnotationElement {
  1647. constructor(parameters) {
  1648. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str || parameters.data.richText?.str);
  1649. super(parameters, {
  1650. isRenderable,
  1651. ignoreBorder: true
  1652. });
  1653. }
  1654. render() {
  1655. this.container.className = "stampAnnotation";
  1656. if (!this.data.hasPopup) {
  1657. this._createPopup(null, this.data);
  1658. }
  1659. return this.container;
  1660. }
  1661. }
  1662. class FileAttachmentAnnotationElement extends AnnotationElement {
  1663. constructor(parameters) {
  1664. super(parameters, {
  1665. isRenderable: true
  1666. });
  1667. const {
  1668. filename,
  1669. content
  1670. } = this.data.file;
  1671. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  1672. this.content = content;
  1673. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  1674. source: this,
  1675. id: (0, _util.stringToPDFString)(filename),
  1676. filename,
  1677. content
  1678. });
  1679. }
  1680. render() {
  1681. this.container.className = "fileAttachmentAnnotation";
  1682. const trigger = document.createElement("div");
  1683. trigger.style.height = this.container.style.height;
  1684. trigger.style.width = this.container.style.width;
  1685. trigger.addEventListener("dblclick", this._download.bind(this));
  1686. if (!this.data.hasPopup && (this.data.titleObj?.str || this.data.contentsObj?.str || this.data.richText)) {
  1687. this._createPopup(trigger, this.data);
  1688. }
  1689. this.container.appendChild(trigger);
  1690. return this.container;
  1691. }
  1692. _download() {
  1693. this.downloadManager?.openOrDownloadData(this.container, this.content, this.filename);
  1694. }
  1695. }
  1696. class AnnotationLayer {
  1697. static render(parameters) {
  1698. const sortedAnnotations = [],
  1699. popupAnnotations = [];
  1700. for (const data of parameters.annotations) {
  1701. if (!data) {
  1702. continue;
  1703. }
  1704. if (data.annotationType === _util.AnnotationType.POPUP) {
  1705. popupAnnotations.push(data);
  1706. continue;
  1707. }
  1708. sortedAnnotations.push(data);
  1709. }
  1710. if (popupAnnotations.length) {
  1711. sortedAnnotations.push(...popupAnnotations);
  1712. }
  1713. const div = parameters.div;
  1714. for (const data of sortedAnnotations) {
  1715. const element = AnnotationElementFactory.create({
  1716. data,
  1717. layer: div,
  1718. page: parameters.page,
  1719. viewport: parameters.viewport,
  1720. linkService: parameters.linkService,
  1721. downloadManager: parameters.downloadManager,
  1722. imageResourcesPath: parameters.imageResourcesPath || "",
  1723. renderForms: parameters.renderForms !== false,
  1724. svgFactory: new _display_utils.DOMSVGFactory(),
  1725. annotationStorage: parameters.annotationStorage || new _annotation_storage.AnnotationStorage(),
  1726. enableScripting: parameters.enableScripting,
  1727. hasJSActions: parameters.hasJSActions,
  1728. fieldObjects: parameters.fieldObjects,
  1729. mouseState: parameters.mouseState || {
  1730. isDown: false
  1731. }
  1732. });
  1733. if (element.isRenderable) {
  1734. const rendered = element.render();
  1735. if (data.hidden) {
  1736. rendered.style.visibility = "hidden";
  1737. }
  1738. if (Array.isArray(rendered)) {
  1739. for (const renderedElement of rendered) {
  1740. div.appendChild(renderedElement);
  1741. }
  1742. } else {
  1743. if (element instanceof PopupAnnotationElement) {
  1744. div.prepend(rendered);
  1745. } else {
  1746. div.appendChild(rendered);
  1747. }
  1748. }
  1749. }
  1750. }
  1751. this.#setAnnotationCanvasMap(div, parameters.annotationCanvasMap);
  1752. }
  1753. static update(parameters) {
  1754. const {
  1755. page,
  1756. viewport,
  1757. annotations,
  1758. annotationCanvasMap,
  1759. div
  1760. } = parameters;
  1761. const transform = viewport.transform;
  1762. const matrix = `matrix(${transform.join(",")})`;
  1763. let scale, ownMatrix;
  1764. for (const data of annotations) {
  1765. const elements = div.querySelectorAll(`[data-annotation-id="${data.id}"]`);
  1766. if (elements) {
  1767. for (const element of elements) {
  1768. if (data.hasOwnCanvas) {
  1769. const rect = _util.Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);
  1770. if (!ownMatrix) {
  1771. scale = Math.abs(transform[0] || transform[1]);
  1772. const ownTransform = transform.slice();
  1773. for (let i = 0; i < 4; i++) {
  1774. ownTransform[i] = Math.sign(ownTransform[i]);
  1775. }
  1776. ownMatrix = `matrix(${ownTransform.join(",")})`;
  1777. }
  1778. const left = rect[0] * scale;
  1779. const top = rect[1] * scale;
  1780. element.style.left = `${left}px`;
  1781. element.style.top = `${top}px`;
  1782. element.style.transformOrigin = `${-left}px ${-top}px`;
  1783. element.style.transform = ownMatrix;
  1784. } else {
  1785. element.style.transform = matrix;
  1786. }
  1787. }
  1788. }
  1789. }
  1790. this.#setAnnotationCanvasMap(div, annotationCanvasMap);
  1791. div.hidden = false;
  1792. }
  1793. static #setAnnotationCanvasMap(div, annotationCanvasMap) {
  1794. if (!annotationCanvasMap) {
  1795. return;
  1796. }
  1797. for (const [id, canvas] of annotationCanvasMap) {
  1798. const element = div.querySelector(`[data-annotation-id="${id}"]`);
  1799. if (!element) {
  1800. continue;
  1801. }
  1802. const {
  1803. firstChild
  1804. } = element;
  1805. if (firstChild.nodeName === "CANVAS") {
  1806. element.replaceChild(canvas, firstChild);
  1807. } else {
  1808. element.insertBefore(canvas, firstChild);
  1809. }
  1810. }
  1811. annotationCanvasMap.clear();
  1812. }
  1813. }
  1814. exports.AnnotationLayer = AnnotationLayer;