2
0

annotation_layer.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 _display_utils = require("./display_utils.js");
  28. var _util = require("../shared/util.js");
  29. var _annotation_storage = require("./annotation_storage.js");
  30. var _scripting_utils = require("../shared/scripting_utils.js");
  31. class AnnotationElementFactory {
  32. static create(parameters) {
  33. const subtype = parameters.data.annotationType;
  34. switch (subtype) {
  35. case _util.AnnotationType.LINK:
  36. return new LinkAnnotationElement(parameters);
  37. case _util.AnnotationType.TEXT:
  38. return new TextAnnotationElement(parameters);
  39. case _util.AnnotationType.WIDGET:
  40. const fieldType = parameters.data.fieldType;
  41. switch (fieldType) {
  42. case "Tx":
  43. return new TextWidgetAnnotationElement(parameters);
  44. case "Btn":
  45. if (parameters.data.radioButton) {
  46. return new RadioButtonWidgetAnnotationElement(parameters);
  47. } else if (parameters.data.checkBox) {
  48. return new CheckboxWidgetAnnotationElement(parameters);
  49. }
  50. return new PushButtonWidgetAnnotationElement(parameters);
  51. case "Ch":
  52. return new ChoiceWidgetAnnotationElement(parameters);
  53. }
  54. return new WidgetAnnotationElement(parameters);
  55. case _util.AnnotationType.POPUP:
  56. return new PopupAnnotationElement(parameters);
  57. case _util.AnnotationType.FREETEXT:
  58. return new FreeTextAnnotationElement(parameters);
  59. case _util.AnnotationType.LINE:
  60. return new LineAnnotationElement(parameters);
  61. case _util.AnnotationType.SQUARE:
  62. return new SquareAnnotationElement(parameters);
  63. case _util.AnnotationType.CIRCLE:
  64. return new CircleAnnotationElement(parameters);
  65. case _util.AnnotationType.POLYLINE:
  66. return new PolylineAnnotationElement(parameters);
  67. case _util.AnnotationType.CARET:
  68. return new CaretAnnotationElement(parameters);
  69. case _util.AnnotationType.INK:
  70. return new InkAnnotationElement(parameters);
  71. case _util.AnnotationType.POLYGON:
  72. return new PolygonAnnotationElement(parameters);
  73. case _util.AnnotationType.HIGHLIGHT:
  74. return new HighlightAnnotationElement(parameters);
  75. case _util.AnnotationType.UNDERLINE:
  76. return new UnderlineAnnotationElement(parameters);
  77. case _util.AnnotationType.SQUIGGLY:
  78. return new SquigglyAnnotationElement(parameters);
  79. case _util.AnnotationType.STRIKEOUT:
  80. return new StrikeOutAnnotationElement(parameters);
  81. case _util.AnnotationType.STAMP:
  82. return new StampAnnotationElement(parameters);
  83. case _util.AnnotationType.FILEATTACHMENT:
  84. return new FileAttachmentAnnotationElement(parameters);
  85. default:
  86. return new AnnotationElement(parameters);
  87. }
  88. }
  89. }
  90. class AnnotationElement {
  91. constructor(parameters, {
  92. isRenderable = false,
  93. ignoreBorder = false,
  94. createQuadrilaterals = false
  95. } = {}) {
  96. this.isRenderable = isRenderable;
  97. this.data = parameters.data;
  98. this.layer = parameters.layer;
  99. this.page = parameters.page;
  100. this.viewport = parameters.viewport;
  101. this.linkService = parameters.linkService;
  102. this.downloadManager = parameters.downloadManager;
  103. this.imageResourcesPath = parameters.imageResourcesPath;
  104. this.renderInteractiveForms = parameters.renderInteractiveForms;
  105. this.svgFactory = parameters.svgFactory;
  106. this.annotationStorage = parameters.annotationStorage;
  107. this.enableScripting = parameters.enableScripting;
  108. this.hasJSActions = parameters.hasJSActions;
  109. this._mouseState = parameters.mouseState;
  110. if (isRenderable) {
  111. this.container = this._createContainer(ignoreBorder);
  112. }
  113. if (createQuadrilaterals) {
  114. this.quadrilaterals = this._createQuadrilaterals(ignoreBorder);
  115. }
  116. }
  117. _createContainer(ignoreBorder = false) {
  118. const data = this.data,
  119. page = this.page,
  120. viewport = this.viewport;
  121. const container = document.createElement("section");
  122. let width = data.rect[2] - data.rect[0];
  123. let height = data.rect[3] - data.rect[1];
  124. container.setAttribute("data-annotation-id", data.id);
  125. 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]]);
  126. container.style.transform = `matrix(${viewport.transform.join(",")})`;
  127. container.style.transformOrigin = `${-rect[0]}px ${-rect[1]}px`;
  128. if (!ignoreBorder && data.borderStyle.width > 0) {
  129. container.style.borderWidth = `${data.borderStyle.width}px`;
  130. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  131. width = width - 2 * data.borderStyle.width;
  132. height = height - 2 * data.borderStyle.width;
  133. }
  134. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  135. const verticalRadius = data.borderStyle.verticalCornerRadius;
  136. if (horizontalRadius > 0 || verticalRadius > 0) {
  137. const radius = `${horizontalRadius}px / ${verticalRadius}px`;
  138. container.style.borderRadius = radius;
  139. }
  140. switch (data.borderStyle.style) {
  141. case _util.AnnotationBorderStyleType.SOLID:
  142. container.style.borderStyle = "solid";
  143. break;
  144. case _util.AnnotationBorderStyleType.DASHED:
  145. container.style.borderStyle = "dashed";
  146. break;
  147. case _util.AnnotationBorderStyleType.BEVELED:
  148. (0, _util.warn)("Unimplemented border style: beveled");
  149. break;
  150. case _util.AnnotationBorderStyleType.INSET:
  151. (0, _util.warn)("Unimplemented border style: inset");
  152. break;
  153. case _util.AnnotationBorderStyleType.UNDERLINE:
  154. container.style.borderBottomStyle = "solid";
  155. break;
  156. default:
  157. break;
  158. }
  159. if (data.color) {
  160. container.style.borderColor = _util.Util.makeHexColor(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  161. } else {
  162. container.style.borderWidth = 0;
  163. }
  164. }
  165. container.style.left = `${rect[0]}px`;
  166. container.style.top = `${rect[1]}px`;
  167. container.style.width = `${width}px`;
  168. container.style.height = `${height}px`;
  169. return container;
  170. }
  171. _createQuadrilaterals(ignoreBorder = false) {
  172. if (!this.data.quadPoints) {
  173. return null;
  174. }
  175. const quadrilaterals = [];
  176. const savedRect = this.data.rect;
  177. for (const quadPoint of this.data.quadPoints) {
  178. this.data.rect = [quadPoint[2].x, quadPoint[2].y, quadPoint[1].x, quadPoint[1].y];
  179. quadrilaterals.push(this._createContainer(ignoreBorder));
  180. }
  181. this.data.rect = savedRect;
  182. return quadrilaterals;
  183. }
  184. _createPopup(trigger, data) {
  185. let container = this.container;
  186. if (this.quadrilaterals) {
  187. trigger = trigger || this.quadrilaterals;
  188. container = this.quadrilaterals[0];
  189. }
  190. if (!trigger) {
  191. trigger = document.createElement("div");
  192. trigger.style.height = container.style.height;
  193. trigger.style.width = container.style.width;
  194. container.appendChild(trigger);
  195. }
  196. const popupElement = new PopupElement({
  197. container,
  198. trigger,
  199. color: data.color,
  200. title: data.title,
  201. modificationDate: data.modificationDate,
  202. contents: data.contents,
  203. hideWrapper: true
  204. });
  205. const popup = popupElement.render();
  206. popup.style.left = container.style.width;
  207. container.appendChild(popup);
  208. }
  209. _renderQuadrilaterals(className) {
  210. this.quadrilaterals.forEach(quadrilateral => {
  211. quadrilateral.className = className;
  212. });
  213. return this.quadrilaterals;
  214. }
  215. render() {
  216. (0, _util.unreachable)("Abstract method `AnnotationElement.render` called");
  217. }
  218. }
  219. class LinkAnnotationElement extends AnnotationElement {
  220. constructor(parameters) {
  221. const isRenderable = !!(parameters.data.url || parameters.data.dest || parameters.data.action || parameters.data.isTooltipOnly || parameters.data.actions && (parameters.data.actions.Action || parameters.data.actions["Mouse Up"] || parameters.data.actions["Mouse Down"]));
  222. super(parameters, {
  223. isRenderable,
  224. createQuadrilaterals: true
  225. });
  226. }
  227. render() {
  228. const {
  229. data,
  230. linkService
  231. } = this;
  232. const link = document.createElement("a");
  233. if (data.url) {
  234. (0, _display_utils.addLinkAttributes)(link, {
  235. url: data.url,
  236. target: data.newWindow ? _display_utils.LinkTarget.BLANK : linkService.externalLinkTarget,
  237. rel: linkService.externalLinkRel,
  238. enabled: linkService.externalLinkEnabled
  239. });
  240. } else if (data.action) {
  241. this._bindNamedAction(link, data.action);
  242. } else if (data.dest) {
  243. this._bindLink(link, data.dest);
  244. } else if (data.actions && (data.actions.Action || data.actions["Mouse Up"] || data.actions["Mouse Down"]) && this.enableScripting && this.hasJSActions) {
  245. this._bindJSAction(link, data);
  246. } else {
  247. this._bindLink(link, "");
  248. }
  249. if (this.quadrilaterals) {
  250. return this._renderQuadrilaterals("linkAnnotation").map((quadrilateral, index) => {
  251. const linkElement = index === 0 ? link : link.cloneNode();
  252. quadrilateral.appendChild(linkElement);
  253. return quadrilateral;
  254. });
  255. }
  256. this.container.className = "linkAnnotation";
  257. this.container.appendChild(link);
  258. return this.container;
  259. }
  260. _bindLink(link, destination) {
  261. link.href = this.linkService.getDestinationHash(destination);
  262. link.onclick = () => {
  263. if (destination) {
  264. this.linkService.goToDestination(destination);
  265. }
  266. return false;
  267. };
  268. if (destination || destination === "") {
  269. link.className = "internalLink";
  270. }
  271. }
  272. _bindNamedAction(link, action) {
  273. link.href = this.linkService.getAnchorUrl("");
  274. link.onclick = () => {
  275. this.linkService.executeNamedAction(action);
  276. return false;
  277. };
  278. link.className = "internalLink";
  279. }
  280. _bindJSAction(link, data) {
  281. link.href = this.linkService.getAnchorUrl("");
  282. const map = new Map([["Action", "onclick"], ["Mouse Up", "onmouseup"], ["Mouse Down", "onmousedown"]]);
  283. for (const name of Object.keys(data.actions)) {
  284. const jsName = map.get(name);
  285. if (!jsName) {
  286. continue;
  287. }
  288. link[jsName] = () => {
  289. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  290. source: this,
  291. detail: {
  292. id: data.id,
  293. name
  294. }
  295. });
  296. return false;
  297. };
  298. }
  299. link.className = "internalLink";
  300. }
  301. }
  302. class TextAnnotationElement extends AnnotationElement {
  303. constructor(parameters) {
  304. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  305. super(parameters, {
  306. isRenderable
  307. });
  308. }
  309. render() {
  310. this.container.className = "textAnnotation";
  311. const image = document.createElement("img");
  312. image.style.height = this.container.style.height;
  313. image.style.width = this.container.style.width;
  314. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  315. image.alt = "[{{type}} Annotation]";
  316. image.dataset.l10nId = "text_annotation_type";
  317. image.dataset.l10nArgs = JSON.stringify({
  318. type: this.data.name
  319. });
  320. if (!this.data.hasPopup) {
  321. this._createPopup(image, this.data);
  322. }
  323. this.container.appendChild(image);
  324. return this.container;
  325. }
  326. }
  327. class WidgetAnnotationElement extends AnnotationElement {
  328. render() {
  329. if (this.data.alternativeText) {
  330. this.container.title = this.data.alternativeText;
  331. }
  332. return this.container;
  333. }
  334. _getKeyModifier(event) {
  335. return navigator.platform.includes("Win") && event.ctrlKey || navigator.platform.includes("Mac") && event.metaKey;
  336. }
  337. _setEventListener(element, baseName, eventName, valueGetter) {
  338. if (baseName.includes("mouse")) {
  339. element.addEventListener(baseName, event => {
  340. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  341. source: this,
  342. detail: {
  343. id: this.data.id,
  344. name: eventName,
  345. value: valueGetter(event),
  346. shift: event.shiftKey,
  347. modifier: this._getKeyModifier(event)
  348. }
  349. });
  350. });
  351. } else {
  352. element.addEventListener(baseName, event => {
  353. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  354. source: this,
  355. detail: {
  356. id: this.data.id,
  357. name: eventName,
  358. value: event.target.checked
  359. }
  360. });
  361. });
  362. }
  363. }
  364. _setEventListeners(element, names, getter) {
  365. for (const [baseName, eventName] of names) {
  366. if (eventName === "Action" || this.data.actions?.[eventName]) {
  367. this._setEventListener(element, baseName, eventName, getter);
  368. }
  369. }
  370. }
  371. }
  372. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  373. constructor(parameters) {
  374. const isRenderable = parameters.renderInteractiveForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  375. super(parameters, {
  376. isRenderable
  377. });
  378. }
  379. render() {
  380. const storage = this.annotationStorage;
  381. const id = this.data.id;
  382. this.container.className = "textWidgetAnnotation";
  383. let element = null;
  384. if (this.renderInteractiveForms) {
  385. const textContent = storage.getOrCreateValue(id, {
  386. value: this.data.fieldValue
  387. }).value;
  388. const elementData = {
  389. userValue: null,
  390. formattedValue: null,
  391. beforeInputSelectionRange: null,
  392. beforeInputValue: null
  393. };
  394. if (this.data.multiLine) {
  395. element = document.createElement("textarea");
  396. element.textContent = textContent;
  397. } else {
  398. element = document.createElement("input");
  399. element.type = "text";
  400. element.setAttribute("value", textContent);
  401. }
  402. elementData.userValue = textContent;
  403. element.setAttribute("id", id);
  404. element.addEventListener("input", function (event) {
  405. storage.setValue(id, {
  406. value: event.target.value
  407. });
  408. });
  409. let blurListener = event => {
  410. if (elementData.formattedValue) {
  411. event.target.value = elementData.formattedValue;
  412. }
  413. event.target.setSelectionRange(0, 0);
  414. elementData.beforeInputSelectionRange = null;
  415. };
  416. if (this.enableScripting && this.hasJSActions) {
  417. element.addEventListener("focus", event => {
  418. if (elementData.userValue) {
  419. event.target.value = elementData.userValue;
  420. }
  421. });
  422. element.addEventListener("updatefromsandbox", function (event) {
  423. const {
  424. detail
  425. } = event;
  426. const actions = {
  427. value() {
  428. elementData.userValue = detail.value || "";
  429. storage.setValue(id, {
  430. value: elementData.userValue.toString()
  431. });
  432. if (!elementData.formattedValue) {
  433. event.target.value = elementData.userValue;
  434. }
  435. },
  436. valueAsString() {
  437. elementData.formattedValue = detail.valueAsString || "";
  438. if (event.target !== document.activeElement) {
  439. event.target.value = elementData.formattedValue;
  440. }
  441. storage.setValue(id, {
  442. formattedValue: elementData.formattedValue
  443. });
  444. },
  445. focus() {
  446. setTimeout(() => event.target.focus({
  447. preventScroll: false
  448. }), 0);
  449. },
  450. userName() {
  451. event.target.title = detail.userName;
  452. },
  453. hidden() {
  454. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  455. storage.setValue(id, {
  456. hidden: detail.hidden
  457. });
  458. },
  459. editable() {
  460. event.target.disabled = !detail.editable;
  461. },
  462. selRange() {
  463. const [selStart, selEnd] = detail.selRange;
  464. if (selStart >= 0 && selEnd < event.target.value.length) {
  465. event.target.setSelectionRange(selStart, selEnd);
  466. }
  467. },
  468. strokeColor() {
  469. const color = detail.strokeColor;
  470. event.target.style.color = _scripting_utils.ColorConverters[`${color[0]}_HTML`](color.slice(1));
  471. }
  472. };
  473. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  474. });
  475. if (this.data.actions) {
  476. element.addEventListener("keydown", event => {
  477. elementData.beforeInputValue = event.target.value;
  478. let commitKey = -1;
  479. if (event.key === "Escape") {
  480. commitKey = 0;
  481. } else if (event.key === "Enter") {
  482. commitKey = 2;
  483. } else if (event.key === "Tab") {
  484. commitKey = 3;
  485. }
  486. if (commitKey === -1) {
  487. return;
  488. }
  489. elementData.userValue = event.target.value;
  490. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  491. source: this,
  492. detail: {
  493. id,
  494. name: "Keystroke",
  495. value: event.target.value,
  496. willCommit: true,
  497. commitKey,
  498. selStart: event.target.selectionStart,
  499. selEnd: event.target.selectionEnd
  500. }
  501. });
  502. });
  503. const _blurListener = blurListener;
  504. blurListener = null;
  505. element.addEventListener("blur", event => {
  506. if (this._mouseState.isDown) {
  507. elementData.userValue = event.target.value;
  508. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  509. source: this,
  510. detail: {
  511. id,
  512. name: "Keystroke",
  513. value: event.target.value,
  514. willCommit: true,
  515. commitKey: 1,
  516. selStart: event.target.selectionStart,
  517. selEnd: event.target.selectionEnd
  518. }
  519. });
  520. }
  521. _blurListener(event);
  522. });
  523. element.addEventListener("mousedown", event => {
  524. elementData.beforeInputValue = event.target.value;
  525. elementData.beforeInputSelectionRange = null;
  526. });
  527. element.addEventListener("keyup", event => {
  528. if (event.target.selectionStart === event.target.selectionEnd) {
  529. elementData.beforeInputSelectionRange = null;
  530. }
  531. });
  532. element.addEventListener("select", event => {
  533. elementData.beforeInputSelectionRange = [event.target.selectionStart, event.target.selectionEnd];
  534. });
  535. if ("Keystroke" in this.data.actions) {
  536. element.addEventListener("input", event => {
  537. let selStart = -1;
  538. let selEnd = -1;
  539. if (elementData.beforeInputSelectionRange) {
  540. [selStart, selEnd] = elementData.beforeInputSelectionRange;
  541. }
  542. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  543. source: this,
  544. detail: {
  545. id,
  546. name: "Keystroke",
  547. value: elementData.beforeInputValue,
  548. change: event.data,
  549. willCommit: false,
  550. selStart,
  551. selEnd
  552. }
  553. });
  554. });
  555. }
  556. this._setEventListeners(element, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  557. }
  558. }
  559. if (blurListener) {
  560. element.addEventListener("blur", blurListener);
  561. }
  562. element.disabled = this.data.readOnly;
  563. element.name = this.data.fieldName;
  564. if (this.data.maxLen !== null) {
  565. element.maxLength = this.data.maxLen;
  566. }
  567. if (this.data.comb) {
  568. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  569. const combWidth = fieldWidth / this.data.maxLen;
  570. element.classList.add("comb");
  571. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  572. }
  573. } else {
  574. element = document.createElement("div");
  575. element.textContent = this.data.fieldValue;
  576. element.style.verticalAlign = "middle";
  577. element.style.display = "table-cell";
  578. }
  579. this._setTextStyle(element);
  580. this.container.appendChild(element);
  581. return this.container;
  582. }
  583. _setTextStyle(element) {
  584. const TEXT_ALIGNMENT = ["left", "center", "right"];
  585. const {
  586. fontSize,
  587. fontColor
  588. } = this.data.defaultAppearanceData;
  589. const style = element.style;
  590. if (fontSize) {
  591. style.fontSize = `${fontSize}px`;
  592. }
  593. style.color = _util.Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  594. if (this.data.textAlignment !== null) {
  595. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  596. }
  597. }
  598. }
  599. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  600. constructor(parameters) {
  601. super(parameters, {
  602. isRenderable: parameters.renderInteractiveForms
  603. });
  604. }
  605. render() {
  606. const storage = this.annotationStorage;
  607. const data = this.data;
  608. const id = data.id;
  609. const value = storage.getOrCreateValue(id, {
  610. value: data.fieldValue && data.fieldValue !== "Off"
  611. }).value;
  612. this.container.className = "buttonWidgetAnnotation checkBox";
  613. const element = document.createElement("input");
  614. element.disabled = data.readOnly;
  615. element.type = "checkbox";
  616. element.name = this.data.fieldName;
  617. if (value) {
  618. element.setAttribute("checked", true);
  619. }
  620. element.setAttribute("id", id);
  621. element.addEventListener("change", function (event) {
  622. const name = event.target.name;
  623. for (const checkbox of document.getElementsByName(name)) {
  624. if (checkbox !== event.target) {
  625. checkbox.checked = false;
  626. storage.setValue(checkbox.parentNode.getAttribute("data-annotation-id"), {
  627. value: false
  628. });
  629. }
  630. }
  631. storage.setValue(id, {
  632. value: event.target.checked
  633. });
  634. });
  635. if (this.enableScripting && this.hasJSActions) {
  636. element.addEventListener("updatefromsandbox", event => {
  637. const {
  638. detail
  639. } = event;
  640. const actions = {
  641. value() {
  642. event.target.checked = detail.value !== "Off";
  643. storage.setValue(id, {
  644. value: event.target.checked
  645. });
  646. },
  647. focus() {
  648. setTimeout(() => event.target.focus({
  649. preventScroll: false
  650. }), 0);
  651. },
  652. hidden() {
  653. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  654. storage.setValue(id, {
  655. hidden: detail.hidden
  656. });
  657. },
  658. editable() {
  659. event.target.disabled = !detail.editable;
  660. }
  661. };
  662. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  663. });
  664. 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);
  665. }
  666. this.container.appendChild(element);
  667. return this.container;
  668. }
  669. }
  670. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  671. constructor(parameters) {
  672. super(parameters, {
  673. isRenderable: parameters.renderInteractiveForms
  674. });
  675. }
  676. render() {
  677. this.container.className = "buttonWidgetAnnotation radioButton";
  678. const storage = this.annotationStorage;
  679. const data = this.data;
  680. const id = data.id;
  681. const value = storage.getOrCreateValue(id, {
  682. value: data.fieldValue === data.buttonValue
  683. }).value;
  684. const element = document.createElement("input");
  685. element.disabled = data.readOnly;
  686. element.type = "radio";
  687. element.name = data.fieldName;
  688. if (value) {
  689. element.setAttribute("checked", true);
  690. }
  691. element.setAttribute("pdfButtonValue", data.buttonValue);
  692. element.setAttribute("id", id);
  693. element.addEventListener("change", function (event) {
  694. const {
  695. target
  696. } = event;
  697. for (const radio of document.getElementsByName(target.name)) {
  698. if (radio !== target) {
  699. storage.setValue(radio.getAttribute("id"), {
  700. value: false
  701. });
  702. }
  703. }
  704. storage.setValue(id, {
  705. value: target.checked
  706. });
  707. });
  708. if (this.enableScripting && this.hasJSActions) {
  709. element.addEventListener("updatefromsandbox", event => {
  710. const {
  711. detail
  712. } = event;
  713. const actions = {
  714. value() {
  715. const fieldValue = detail.value;
  716. for (const radio of document.getElementsByName(event.target.name)) {
  717. const radioId = radio.getAttribute("id");
  718. if (fieldValue === radio.getAttribute("pdfButtonValue")) {
  719. radio.setAttribute("checked", true);
  720. storage.setValue(radioId, {
  721. value: true
  722. });
  723. } else {
  724. storage.setValue(radioId, {
  725. value: false
  726. });
  727. }
  728. }
  729. },
  730. focus() {
  731. setTimeout(() => event.target.focus({
  732. preventScroll: false
  733. }), 0);
  734. },
  735. hidden() {
  736. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  737. storage.setValue(id, {
  738. hidden: detail.hidden
  739. });
  740. },
  741. editable() {
  742. event.target.disabled = !detail.editable;
  743. }
  744. };
  745. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  746. });
  747. 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);
  748. }
  749. this.container.appendChild(element);
  750. return this.container;
  751. }
  752. }
  753. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  754. render() {
  755. const container = super.render();
  756. container.className = "buttonWidgetAnnotation pushButton";
  757. if (this.data.alternativeText) {
  758. container.title = this.data.alternativeText;
  759. }
  760. return container;
  761. }
  762. }
  763. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  764. constructor(parameters) {
  765. super(parameters, {
  766. isRenderable: parameters.renderInteractiveForms
  767. });
  768. }
  769. render() {
  770. this.container.className = "choiceWidgetAnnotation";
  771. const storage = this.annotationStorage;
  772. const id = this.data.id;
  773. storage.getOrCreateValue(id, {
  774. value: this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : undefined
  775. });
  776. const selectElement = document.createElement("select");
  777. selectElement.disabled = this.data.readOnly;
  778. selectElement.name = this.data.fieldName;
  779. selectElement.setAttribute("id", id);
  780. if (!this.data.combo) {
  781. selectElement.size = this.data.options.length;
  782. if (this.data.multiSelect) {
  783. selectElement.multiple = true;
  784. }
  785. }
  786. for (const option of this.data.options) {
  787. const optionElement = document.createElement("option");
  788. optionElement.textContent = option.displayValue;
  789. optionElement.value = option.exportValue;
  790. if (this.data.fieldValue.includes(option.exportValue)) {
  791. optionElement.setAttribute("selected", true);
  792. }
  793. selectElement.appendChild(optionElement);
  794. }
  795. function getValue(event) {
  796. const options = event.target.options;
  797. return options[options.selectedIndex].value;
  798. }
  799. if (this.enableScripting && this.hasJSActions) {
  800. selectElement.addEventListener("updatefromsandbox", event => {
  801. const {
  802. detail
  803. } = event;
  804. const actions = {
  805. value() {
  806. const options = event.target.options;
  807. const value = detail.value;
  808. const i = options.indexOf(value);
  809. if (i !== -1) {
  810. options.selectedIndex = i;
  811. storage.setValue(id, {
  812. value
  813. });
  814. }
  815. },
  816. focus() {
  817. setTimeout(() => event.target.focus({
  818. preventScroll: false
  819. }), 0);
  820. },
  821. hidden() {
  822. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  823. storage.setValue(id, {
  824. hidden: detail.hidden
  825. });
  826. },
  827. editable() {
  828. event.target.disabled = !detail.editable;
  829. }
  830. };
  831. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  832. });
  833. selectElement.addEventListener("input", event => {
  834. const value = getValue(event);
  835. storage.setValue(id, {
  836. value
  837. });
  838. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  839. source: this,
  840. detail: {
  841. id,
  842. name: "Keystroke",
  843. changeEx: value,
  844. willCommit: true,
  845. commitKey: 1,
  846. keyDown: false
  847. }
  848. });
  849. });
  850. this._setEventListeners(selectElement, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  851. } else {
  852. selectElement.addEventListener("input", function (event) {
  853. storage.setValue(id, {
  854. value: getValue(event)
  855. });
  856. });
  857. }
  858. this.container.appendChild(selectElement);
  859. return this.container;
  860. }
  861. }
  862. class PopupAnnotationElement extends AnnotationElement {
  863. constructor(parameters) {
  864. const isRenderable = !!(parameters.data.title || parameters.data.contents);
  865. super(parameters, {
  866. isRenderable
  867. });
  868. }
  869. render() {
  870. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  871. this.container.className = "popupAnnotation";
  872. if (IGNORE_TYPES.includes(this.data.parentType)) {
  873. return this.container;
  874. }
  875. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  876. const parentElements = this.layer.querySelectorAll(selector);
  877. if (parentElements.length === 0) {
  878. return this.container;
  879. }
  880. const popup = new PopupElement({
  881. container: this.container,
  882. trigger: Array.from(parentElements),
  883. color: this.data.color,
  884. title: this.data.title,
  885. modificationDate: this.data.modificationDate,
  886. contents: this.data.contents
  887. });
  888. const page = this.page;
  889. 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]]);
  890. const popupLeft = rect[0] + this.data.parentRect[2] - this.data.parentRect[0];
  891. const popupTop = rect[1];
  892. this.container.style.transformOrigin = `${-popupLeft}px ${-popupTop}px`;
  893. this.container.style.left = `${popupLeft}px`;
  894. this.container.style.top = `${popupTop}px`;
  895. this.container.appendChild(popup.render());
  896. return this.container;
  897. }
  898. }
  899. class PopupElement {
  900. constructor(parameters) {
  901. this.container = parameters.container;
  902. this.trigger = parameters.trigger;
  903. this.color = parameters.color;
  904. this.title = parameters.title;
  905. this.modificationDate = parameters.modificationDate;
  906. this.contents = parameters.contents;
  907. this.hideWrapper = parameters.hideWrapper || false;
  908. this.pinned = false;
  909. }
  910. render() {
  911. const BACKGROUND_ENLIGHT = 0.7;
  912. const wrapper = document.createElement("div");
  913. wrapper.className = "popupWrapper";
  914. this.hideElement = this.hideWrapper ? wrapper : this.container;
  915. this.hideElement.setAttribute("hidden", true);
  916. const popup = document.createElement("div");
  917. popup.className = "popup";
  918. const color = this.color;
  919. if (color) {
  920. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  921. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  922. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  923. popup.style.backgroundColor = _util.Util.makeHexColor(r | 0, g | 0, b | 0);
  924. }
  925. const title = document.createElement("h1");
  926. title.textContent = this.title;
  927. popup.appendChild(title);
  928. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  929. if (dateObject) {
  930. const modificationDate = document.createElement("span");
  931. modificationDate.textContent = "{{date}}, {{time}}";
  932. modificationDate.dataset.l10nId = "annotation_date_string";
  933. modificationDate.dataset.l10nArgs = JSON.stringify({
  934. date: dateObject.toLocaleDateString(),
  935. time: dateObject.toLocaleTimeString()
  936. });
  937. popup.appendChild(modificationDate);
  938. }
  939. const contents = this._formatContents(this.contents);
  940. popup.appendChild(contents);
  941. if (!Array.isArray(this.trigger)) {
  942. this.trigger = [this.trigger];
  943. }
  944. this.trigger.forEach(element => {
  945. element.addEventListener("click", this._toggle.bind(this));
  946. element.addEventListener("mouseover", this._show.bind(this, false));
  947. element.addEventListener("mouseout", this._hide.bind(this, false));
  948. });
  949. popup.addEventListener("click", this._hide.bind(this, true));
  950. wrapper.appendChild(popup);
  951. return wrapper;
  952. }
  953. _formatContents(contents) {
  954. const p = document.createElement("p");
  955. const lines = contents.split(/(?:\r\n?|\n)/);
  956. for (let i = 0, ii = lines.length; i < ii; ++i) {
  957. const line = lines[i];
  958. p.appendChild(document.createTextNode(line));
  959. if (i < ii - 1) {
  960. p.appendChild(document.createElement("br"));
  961. }
  962. }
  963. return p;
  964. }
  965. _toggle() {
  966. if (this.pinned) {
  967. this._hide(true);
  968. } else {
  969. this._show(true);
  970. }
  971. }
  972. _show(pin = false) {
  973. if (pin) {
  974. this.pinned = true;
  975. }
  976. if (this.hideElement.hasAttribute("hidden")) {
  977. this.hideElement.removeAttribute("hidden");
  978. this.container.style.zIndex += 1;
  979. }
  980. }
  981. _hide(unpin = true) {
  982. if (unpin) {
  983. this.pinned = false;
  984. }
  985. if (!this.hideElement.hasAttribute("hidden") && !this.pinned) {
  986. this.hideElement.setAttribute("hidden", true);
  987. this.container.style.zIndex -= 1;
  988. }
  989. }
  990. }
  991. class FreeTextAnnotationElement extends AnnotationElement {
  992. constructor(parameters) {
  993. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  994. super(parameters, {
  995. isRenderable,
  996. ignoreBorder: true
  997. });
  998. }
  999. render() {
  1000. this.container.className = "freeTextAnnotation";
  1001. if (!this.data.hasPopup) {
  1002. this._createPopup(null, this.data);
  1003. }
  1004. return this.container;
  1005. }
  1006. }
  1007. class LineAnnotationElement extends AnnotationElement {
  1008. constructor(parameters) {
  1009. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1010. super(parameters, {
  1011. isRenderable,
  1012. ignoreBorder: true
  1013. });
  1014. }
  1015. render() {
  1016. this.container.className = "lineAnnotation";
  1017. const data = this.data;
  1018. const width = data.rect[2] - data.rect[0];
  1019. const height = data.rect[3] - data.rect[1];
  1020. const svg = this.svgFactory.create(width, height);
  1021. const line = this.svgFactory.createElement("svg:line");
  1022. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  1023. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  1024. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  1025. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  1026. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  1027. line.setAttribute("stroke", "transparent");
  1028. svg.appendChild(line);
  1029. this.container.append(svg);
  1030. this._createPopup(line, data);
  1031. return this.container;
  1032. }
  1033. }
  1034. class SquareAnnotationElement extends AnnotationElement {
  1035. constructor(parameters) {
  1036. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1037. super(parameters, {
  1038. isRenderable,
  1039. ignoreBorder: true
  1040. });
  1041. }
  1042. render() {
  1043. this.container.className = "squareAnnotation";
  1044. const data = this.data;
  1045. const width = data.rect[2] - data.rect[0];
  1046. const height = data.rect[3] - data.rect[1];
  1047. const svg = this.svgFactory.create(width, height);
  1048. const borderWidth = data.borderStyle.width;
  1049. const square = this.svgFactory.createElement("svg:rect");
  1050. square.setAttribute("x", borderWidth / 2);
  1051. square.setAttribute("y", borderWidth / 2);
  1052. square.setAttribute("width", width - borderWidth);
  1053. square.setAttribute("height", height - borderWidth);
  1054. square.setAttribute("stroke-width", borderWidth || 1);
  1055. square.setAttribute("stroke", "transparent");
  1056. square.setAttribute("fill", "none");
  1057. svg.appendChild(square);
  1058. this.container.append(svg);
  1059. this._createPopup(square, data);
  1060. return this.container;
  1061. }
  1062. }
  1063. class CircleAnnotationElement extends AnnotationElement {
  1064. constructor(parameters) {
  1065. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1066. super(parameters, {
  1067. isRenderable,
  1068. ignoreBorder: true
  1069. });
  1070. }
  1071. render() {
  1072. this.container.className = "circleAnnotation";
  1073. const data = this.data;
  1074. const width = data.rect[2] - data.rect[0];
  1075. const height = data.rect[3] - data.rect[1];
  1076. const svg = this.svgFactory.create(width, height);
  1077. const borderWidth = data.borderStyle.width;
  1078. const circle = this.svgFactory.createElement("svg:ellipse");
  1079. circle.setAttribute("cx", width / 2);
  1080. circle.setAttribute("cy", height / 2);
  1081. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  1082. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  1083. circle.setAttribute("stroke-width", borderWidth || 1);
  1084. circle.setAttribute("stroke", "transparent");
  1085. circle.setAttribute("fill", "none");
  1086. svg.appendChild(circle);
  1087. this.container.append(svg);
  1088. this._createPopup(circle, data);
  1089. return this.container;
  1090. }
  1091. }
  1092. class PolylineAnnotationElement extends AnnotationElement {
  1093. constructor(parameters) {
  1094. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1095. super(parameters, {
  1096. isRenderable,
  1097. ignoreBorder: true
  1098. });
  1099. this.containerClassName = "polylineAnnotation";
  1100. this.svgElementName = "svg:polyline";
  1101. }
  1102. render() {
  1103. this.container.className = this.containerClassName;
  1104. const data = this.data;
  1105. const width = data.rect[2] - data.rect[0];
  1106. const height = data.rect[3] - data.rect[1];
  1107. const svg = this.svgFactory.create(width, height);
  1108. let points = [];
  1109. for (const coordinate of data.vertices) {
  1110. const x = coordinate.x - data.rect[0];
  1111. const y = data.rect[3] - coordinate.y;
  1112. points.push(x + "," + y);
  1113. }
  1114. points = points.join(" ");
  1115. const polyline = this.svgFactory.createElement(this.svgElementName);
  1116. polyline.setAttribute("points", points);
  1117. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1118. polyline.setAttribute("stroke", "transparent");
  1119. polyline.setAttribute("fill", "none");
  1120. svg.appendChild(polyline);
  1121. this.container.append(svg);
  1122. this._createPopup(polyline, data);
  1123. return this.container;
  1124. }
  1125. }
  1126. class PolygonAnnotationElement extends PolylineAnnotationElement {
  1127. constructor(parameters) {
  1128. super(parameters);
  1129. this.containerClassName = "polygonAnnotation";
  1130. this.svgElementName = "svg:polygon";
  1131. }
  1132. }
  1133. class CaretAnnotationElement extends AnnotationElement {
  1134. constructor(parameters) {
  1135. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1136. super(parameters, {
  1137. isRenderable,
  1138. ignoreBorder: true
  1139. });
  1140. }
  1141. render() {
  1142. this.container.className = "caretAnnotation";
  1143. if (!this.data.hasPopup) {
  1144. this._createPopup(null, this.data);
  1145. }
  1146. return this.container;
  1147. }
  1148. }
  1149. class InkAnnotationElement extends AnnotationElement {
  1150. constructor(parameters) {
  1151. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1152. super(parameters, {
  1153. isRenderable,
  1154. ignoreBorder: true
  1155. });
  1156. this.containerClassName = "inkAnnotation";
  1157. this.svgElementName = "svg:polyline";
  1158. }
  1159. render() {
  1160. this.container.className = this.containerClassName;
  1161. const data = this.data;
  1162. const width = data.rect[2] - data.rect[0];
  1163. const height = data.rect[3] - data.rect[1];
  1164. const svg = this.svgFactory.create(width, height);
  1165. for (const inkList of data.inkLists) {
  1166. let points = [];
  1167. for (const coordinate of inkList) {
  1168. const x = coordinate.x - data.rect[0];
  1169. const y = data.rect[3] - coordinate.y;
  1170. points.push(`${x},${y}`);
  1171. }
  1172. points = points.join(" ");
  1173. const polyline = this.svgFactory.createElement(this.svgElementName);
  1174. polyline.setAttribute("points", points);
  1175. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1176. polyline.setAttribute("stroke", "transparent");
  1177. polyline.setAttribute("fill", "none");
  1178. this._createPopup(polyline, data);
  1179. svg.appendChild(polyline);
  1180. }
  1181. this.container.append(svg);
  1182. return this.container;
  1183. }
  1184. }
  1185. class HighlightAnnotationElement extends AnnotationElement {
  1186. constructor(parameters) {
  1187. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1188. super(parameters, {
  1189. isRenderable,
  1190. ignoreBorder: true,
  1191. createQuadrilaterals: true
  1192. });
  1193. }
  1194. render() {
  1195. if (!this.data.hasPopup) {
  1196. this._createPopup(null, this.data);
  1197. }
  1198. if (this.quadrilaterals) {
  1199. return this._renderQuadrilaterals("highlightAnnotation");
  1200. }
  1201. this.container.className = "highlightAnnotation";
  1202. return this.container;
  1203. }
  1204. }
  1205. class UnderlineAnnotationElement extends AnnotationElement {
  1206. constructor(parameters) {
  1207. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1208. super(parameters, {
  1209. isRenderable,
  1210. ignoreBorder: true,
  1211. createQuadrilaterals: true
  1212. });
  1213. }
  1214. render() {
  1215. if (!this.data.hasPopup) {
  1216. this._createPopup(null, this.data);
  1217. }
  1218. if (this.quadrilaterals) {
  1219. return this._renderQuadrilaterals("underlineAnnotation");
  1220. }
  1221. this.container.className = "underlineAnnotation";
  1222. return this.container;
  1223. }
  1224. }
  1225. class SquigglyAnnotationElement extends AnnotationElement {
  1226. constructor(parameters) {
  1227. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1228. super(parameters, {
  1229. isRenderable,
  1230. ignoreBorder: true,
  1231. createQuadrilaterals: true
  1232. });
  1233. }
  1234. render() {
  1235. if (!this.data.hasPopup) {
  1236. this._createPopup(null, this.data);
  1237. }
  1238. if (this.quadrilaterals) {
  1239. return this._renderQuadrilaterals("squigglyAnnotation");
  1240. }
  1241. this.container.className = "squigglyAnnotation";
  1242. return this.container;
  1243. }
  1244. }
  1245. class StrikeOutAnnotationElement extends AnnotationElement {
  1246. constructor(parameters) {
  1247. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1248. super(parameters, {
  1249. isRenderable,
  1250. ignoreBorder: true,
  1251. createQuadrilaterals: true
  1252. });
  1253. }
  1254. render() {
  1255. if (!this.data.hasPopup) {
  1256. this._createPopup(null, this.data);
  1257. }
  1258. if (this.quadrilaterals) {
  1259. return this._renderQuadrilaterals("strikeoutAnnotation");
  1260. }
  1261. this.container.className = "strikeoutAnnotation";
  1262. return this.container;
  1263. }
  1264. }
  1265. class StampAnnotationElement extends AnnotationElement {
  1266. constructor(parameters) {
  1267. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1268. super(parameters, {
  1269. isRenderable,
  1270. ignoreBorder: true
  1271. });
  1272. }
  1273. render() {
  1274. this.container.className = "stampAnnotation";
  1275. if (!this.data.hasPopup) {
  1276. this._createPopup(null, this.data);
  1277. }
  1278. return this.container;
  1279. }
  1280. }
  1281. class FileAttachmentAnnotationElement extends AnnotationElement {
  1282. constructor(parameters) {
  1283. super(parameters, {
  1284. isRenderable: true
  1285. });
  1286. const {
  1287. filename,
  1288. content
  1289. } = this.data.file;
  1290. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  1291. this.content = content;
  1292. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  1293. source: this,
  1294. id: (0, _util.stringToPDFString)(filename),
  1295. filename,
  1296. content
  1297. });
  1298. }
  1299. render() {
  1300. this.container.className = "fileAttachmentAnnotation";
  1301. const trigger = document.createElement("div");
  1302. trigger.style.height = this.container.style.height;
  1303. trigger.style.width = this.container.style.width;
  1304. trigger.addEventListener("dblclick", this._download.bind(this));
  1305. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  1306. this._createPopup(trigger, this.data);
  1307. }
  1308. this.container.appendChild(trigger);
  1309. return this.container;
  1310. }
  1311. _download() {
  1312. if (!this.downloadManager) {
  1313. (0, _util.warn)("Download cannot be started due to unavailable download manager");
  1314. return;
  1315. }
  1316. this.downloadManager.downloadData(this.content, this.filename, "");
  1317. }
  1318. }
  1319. class AnnotationLayer {
  1320. static render(parameters) {
  1321. const sortedAnnotations = [],
  1322. popupAnnotations = [];
  1323. for (const data of parameters.annotations) {
  1324. if (!data) {
  1325. continue;
  1326. }
  1327. if (data.annotationType === _util.AnnotationType.POPUP) {
  1328. popupAnnotations.push(data);
  1329. continue;
  1330. }
  1331. sortedAnnotations.push(data);
  1332. }
  1333. if (popupAnnotations.length) {
  1334. sortedAnnotations.push(...popupAnnotations);
  1335. }
  1336. for (const data of sortedAnnotations) {
  1337. const element = AnnotationElementFactory.create({
  1338. data,
  1339. layer: parameters.div,
  1340. page: parameters.page,
  1341. viewport: parameters.viewport,
  1342. linkService: parameters.linkService,
  1343. downloadManager: parameters.downloadManager,
  1344. imageResourcesPath: parameters.imageResourcesPath || "",
  1345. renderInteractiveForms: typeof parameters.renderInteractiveForms === "boolean" ? parameters.renderInteractiveForms : true,
  1346. svgFactory: new _display_utils.DOMSVGFactory(),
  1347. annotationStorage: parameters.annotationStorage || new _annotation_storage.AnnotationStorage(),
  1348. enableScripting: parameters.enableScripting,
  1349. hasJSActions: parameters.hasJSActions,
  1350. mouseState: parameters.mouseState || {
  1351. isDown: false
  1352. }
  1353. });
  1354. if (element.isRenderable) {
  1355. const rendered = element.render();
  1356. if (data.hidden) {
  1357. rendered.style.visibility = "hidden";
  1358. }
  1359. if (Array.isArray(rendered)) {
  1360. for (const renderedElement of rendered) {
  1361. parameters.div.appendChild(renderedElement);
  1362. }
  1363. } else {
  1364. if (element instanceof PopupAnnotationElement) {
  1365. parameters.div.prepend(rendered);
  1366. } else {
  1367. parameters.div.appendChild(rendered);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. static update(parameters) {
  1374. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  1375. for (const data of parameters.annotations) {
  1376. const elements = parameters.div.querySelectorAll(`[data-annotation-id="${data.id}"]`);
  1377. if (elements) {
  1378. elements.forEach(element => {
  1379. element.style.transform = transform;
  1380. });
  1381. }
  1382. }
  1383. parameters.div.removeAttribute("hidden");
  1384. }
  1385. }
  1386. exports.AnnotationLayer = AnnotationLayer;