annotation_layer.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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 _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. _setColor(event) {
  372. const {
  373. detail,
  374. target
  375. } = event;
  376. const {
  377. style
  378. } = target;
  379. for (const name of ["bgColor", "fillColor", "fgColor", "textColor", "borderColor", "strokeColor"]) {
  380. let color = detail[name];
  381. if (!color) {
  382. continue;
  383. }
  384. color = _scripting_utils.ColorConverters[`${color[0]}_HTML`](color.slice(1));
  385. switch (name) {
  386. case "bgColor":
  387. case "fillColor":
  388. style.backgroundColor = color;
  389. break;
  390. case "fgColor":
  391. case "textColor":
  392. style.color = color;
  393. break;
  394. case "borderColor":
  395. case "strokeColor":
  396. style.borderColor = color;
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  403. constructor(parameters) {
  404. const isRenderable = parameters.renderInteractiveForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  405. super(parameters, {
  406. isRenderable
  407. });
  408. }
  409. render() {
  410. const storage = this.annotationStorage;
  411. const id = this.data.id;
  412. this.container.className = "textWidgetAnnotation";
  413. let element = null;
  414. if (this.renderInteractiveForms) {
  415. const storedData = storage.getValue(id, {
  416. value: this.data.fieldValue,
  417. valueAsString: this.data.fieldValue
  418. });
  419. const textContent = storedData.valueAsString || storedData.value || "";
  420. const elementData = {
  421. userValue: null,
  422. formattedValue: null,
  423. beforeInputSelectionRange: null,
  424. beforeInputValue: null
  425. };
  426. if (this.data.multiLine) {
  427. element = document.createElement("textarea");
  428. element.textContent = textContent;
  429. } else {
  430. element = document.createElement("input");
  431. element.type = "text";
  432. element.setAttribute("value", textContent);
  433. }
  434. elementData.userValue = textContent;
  435. element.setAttribute("id", id);
  436. element.addEventListener("input", function (event) {
  437. storage.setValue(id, {
  438. value: event.target.value
  439. });
  440. });
  441. let blurListener = event => {
  442. if (elementData.formattedValue) {
  443. event.target.value = elementData.formattedValue;
  444. }
  445. event.target.setSelectionRange(0, 0);
  446. elementData.beforeInputSelectionRange = null;
  447. };
  448. if (this.enableScripting && this.hasJSActions) {
  449. element.addEventListener("focus", event => {
  450. if (elementData.userValue) {
  451. event.target.value = elementData.userValue;
  452. }
  453. });
  454. element.addEventListener("updatefromsandbox", event => {
  455. const {
  456. detail
  457. } = event;
  458. const actions = {
  459. value() {
  460. elementData.userValue = detail.value || "";
  461. storage.setValue(id, {
  462. value: elementData.userValue.toString()
  463. });
  464. if (!elementData.formattedValue) {
  465. event.target.value = elementData.userValue;
  466. }
  467. },
  468. valueAsString() {
  469. elementData.formattedValue = detail.valueAsString || "";
  470. if (event.target !== document.activeElement) {
  471. event.target.value = elementData.formattedValue;
  472. }
  473. storage.setValue(id, {
  474. formattedValue: elementData.formattedValue
  475. });
  476. },
  477. focus() {
  478. setTimeout(() => event.target.focus({
  479. preventScroll: false
  480. }), 0);
  481. },
  482. userName() {
  483. event.target.title = detail.userName;
  484. },
  485. hidden() {
  486. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  487. storage.setValue(id, {
  488. hidden: detail.hidden
  489. });
  490. },
  491. editable() {
  492. event.target.disabled = !detail.editable;
  493. },
  494. selRange() {
  495. const [selStart, selEnd] = detail.selRange;
  496. if (selStart >= 0 && selEnd < event.target.value.length) {
  497. event.target.setSelectionRange(selStart, selEnd);
  498. }
  499. }
  500. };
  501. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  502. this._setColor(event);
  503. });
  504. element.addEventListener("keydown", event => {
  505. elementData.beforeInputValue = event.target.value;
  506. let commitKey = -1;
  507. if (event.key === "Escape") {
  508. commitKey = 0;
  509. } else if (event.key === "Enter") {
  510. commitKey = 2;
  511. } else if (event.key === "Tab") {
  512. commitKey = 3;
  513. }
  514. if (commitKey === -1) {
  515. return;
  516. }
  517. elementData.userValue = event.target.value;
  518. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  519. source: this,
  520. detail: {
  521. id,
  522. name: "Keystroke",
  523. value: event.target.value,
  524. willCommit: true,
  525. commitKey,
  526. selStart: event.target.selectionStart,
  527. selEnd: event.target.selectionEnd
  528. }
  529. });
  530. });
  531. const _blurListener = blurListener;
  532. blurListener = null;
  533. element.addEventListener("blur", event => {
  534. if (this._mouseState.isDown) {
  535. elementData.userValue = event.target.value;
  536. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  537. source: this,
  538. detail: {
  539. id,
  540. name: "Keystroke",
  541. value: event.target.value,
  542. willCommit: true,
  543. commitKey: 1,
  544. selStart: event.target.selectionStart,
  545. selEnd: event.target.selectionEnd
  546. }
  547. });
  548. }
  549. _blurListener(event);
  550. });
  551. element.addEventListener("mousedown", event => {
  552. elementData.beforeInputValue = event.target.value;
  553. elementData.beforeInputSelectionRange = null;
  554. });
  555. element.addEventListener("keyup", event => {
  556. if (event.target.selectionStart === event.target.selectionEnd) {
  557. elementData.beforeInputSelectionRange = null;
  558. }
  559. });
  560. element.addEventListener("select", event => {
  561. elementData.beforeInputSelectionRange = [event.target.selectionStart, event.target.selectionEnd];
  562. });
  563. if (this.data.actions?.Keystroke) {
  564. element.addEventListener("input", event => {
  565. let selStart = -1;
  566. let selEnd = -1;
  567. if (elementData.beforeInputSelectionRange) {
  568. [selStart, selEnd] = elementData.beforeInputSelectionRange;
  569. }
  570. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  571. source: this,
  572. detail: {
  573. id,
  574. name: "Keystroke",
  575. value: elementData.beforeInputValue,
  576. change: event.data,
  577. willCommit: false,
  578. selStart,
  579. selEnd
  580. }
  581. });
  582. });
  583. }
  584. this._setEventListeners(element, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  585. }
  586. if (blurListener) {
  587. element.addEventListener("blur", blurListener);
  588. }
  589. element.disabled = this.data.readOnly;
  590. element.name = this.data.fieldName;
  591. if (this.data.maxLen !== null) {
  592. element.maxLength = this.data.maxLen;
  593. }
  594. if (this.data.comb) {
  595. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  596. const combWidth = fieldWidth / this.data.maxLen;
  597. element.classList.add("comb");
  598. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  599. }
  600. } else {
  601. element = document.createElement("div");
  602. element.textContent = this.data.fieldValue;
  603. element.style.verticalAlign = "middle";
  604. element.style.display = "table-cell";
  605. }
  606. this._setTextStyle(element);
  607. this.container.appendChild(element);
  608. return this.container;
  609. }
  610. _setTextStyle(element) {
  611. const TEXT_ALIGNMENT = ["left", "center", "right"];
  612. const {
  613. fontSize,
  614. fontColor
  615. } = this.data.defaultAppearanceData;
  616. const style = element.style;
  617. if (fontSize) {
  618. style.fontSize = `${fontSize}px`;
  619. }
  620. style.color = _util.Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  621. if (this.data.textAlignment !== null) {
  622. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  623. }
  624. }
  625. }
  626. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  627. constructor(parameters) {
  628. super(parameters, {
  629. isRenderable: parameters.renderInteractiveForms
  630. });
  631. }
  632. render() {
  633. const storage = this.annotationStorage;
  634. const data = this.data;
  635. const id = data.id;
  636. const value = storage.getValue(id, {
  637. value: data.fieldValue && (data.exportValue && data.exportValue === data.fieldValue || !data.exportValue && data.fieldValue !== "Off")
  638. }).value;
  639. this.container.className = "buttonWidgetAnnotation checkBox";
  640. const element = document.createElement("input");
  641. element.disabled = data.readOnly;
  642. element.type = "checkbox";
  643. element.name = this.data.fieldName;
  644. if (value) {
  645. element.setAttribute("checked", true);
  646. }
  647. element.setAttribute("id", id);
  648. element.addEventListener("change", function (event) {
  649. const name = event.target.name;
  650. for (const checkbox of document.getElementsByName(name)) {
  651. if (checkbox !== event.target) {
  652. checkbox.checked = false;
  653. storage.setValue(checkbox.parentNode.getAttribute("data-annotation-id"), {
  654. value: false
  655. });
  656. }
  657. }
  658. storage.setValue(id, {
  659. value: event.target.checked
  660. });
  661. });
  662. if (this.enableScripting && this.hasJSActions) {
  663. element.addEventListener("updatefromsandbox", event => {
  664. const {
  665. detail
  666. } = event;
  667. const actions = {
  668. value() {
  669. event.target.checked = detail.value !== "Off";
  670. storage.setValue(id, {
  671. value: event.target.checked
  672. });
  673. },
  674. focus() {
  675. setTimeout(() => event.target.focus({
  676. preventScroll: false
  677. }), 0);
  678. },
  679. hidden() {
  680. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  681. storage.setValue(id, {
  682. hidden: detail.hidden
  683. });
  684. },
  685. editable() {
  686. event.target.disabled = !detail.editable;
  687. }
  688. };
  689. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  690. this._setColor(event);
  691. });
  692. 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);
  693. }
  694. this.container.appendChild(element);
  695. return this.container;
  696. }
  697. }
  698. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  699. constructor(parameters) {
  700. super(parameters, {
  701. isRenderable: parameters.renderInteractiveForms
  702. });
  703. }
  704. render() {
  705. this.container.className = "buttonWidgetAnnotation radioButton";
  706. const storage = this.annotationStorage;
  707. const data = this.data;
  708. const id = data.id;
  709. const value = storage.getValue(id, {
  710. value: data.fieldValue === data.buttonValue
  711. }).value;
  712. const element = document.createElement("input");
  713. element.disabled = data.readOnly;
  714. element.type = "radio";
  715. element.name = data.fieldName;
  716. if (value) {
  717. element.setAttribute("checked", true);
  718. }
  719. element.setAttribute("id", id);
  720. element.addEventListener("change", function (event) {
  721. const {
  722. target
  723. } = event;
  724. for (const radio of document.getElementsByName(target.name)) {
  725. if (radio !== target) {
  726. storage.setValue(radio.getAttribute("id"), {
  727. value: false
  728. });
  729. }
  730. }
  731. storage.setValue(id, {
  732. value: target.checked
  733. });
  734. });
  735. if (this.enableScripting && this.hasJSActions) {
  736. const pdfButtonValue = data.buttonValue;
  737. element.addEventListener("updatefromsandbox", event => {
  738. const {
  739. detail
  740. } = event;
  741. const actions = {
  742. value() {
  743. const checked = pdfButtonValue === detail.value;
  744. for (const radio of document.getElementsByName(event.target.name)) {
  745. const radioId = radio.getAttribute("id");
  746. radio.checked = radioId === id && checked;
  747. storage.setValue(radioId, {
  748. value: radio.checked
  749. });
  750. }
  751. },
  752. focus() {
  753. setTimeout(() => event.target.focus({
  754. preventScroll: false
  755. }), 0);
  756. },
  757. hidden() {
  758. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  759. storage.setValue(id, {
  760. hidden: detail.hidden
  761. });
  762. },
  763. editable() {
  764. event.target.disabled = !detail.editable;
  765. }
  766. };
  767. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  768. this._setColor(event);
  769. });
  770. 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);
  771. }
  772. this.container.appendChild(element);
  773. return this.container;
  774. }
  775. }
  776. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  777. render() {
  778. const container = super.render();
  779. container.className = "buttonWidgetAnnotation pushButton";
  780. if (this.data.alternativeText) {
  781. container.title = this.data.alternativeText;
  782. }
  783. return container;
  784. }
  785. }
  786. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  787. constructor(parameters) {
  788. super(parameters, {
  789. isRenderable: parameters.renderInteractiveForms
  790. });
  791. }
  792. render() {
  793. this.container.className = "choiceWidgetAnnotation";
  794. const storage = this.annotationStorage;
  795. const id = this.data.id;
  796. storage.getValue(id, {
  797. value: this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : undefined
  798. });
  799. const selectElement = document.createElement("select");
  800. selectElement.disabled = this.data.readOnly;
  801. selectElement.name = this.data.fieldName;
  802. selectElement.setAttribute("id", id);
  803. if (!this.data.combo) {
  804. selectElement.size = this.data.options.length;
  805. if (this.data.multiSelect) {
  806. selectElement.multiple = true;
  807. }
  808. }
  809. for (const option of this.data.options) {
  810. const optionElement = document.createElement("option");
  811. optionElement.textContent = option.displayValue;
  812. optionElement.value = option.exportValue;
  813. if (this.data.fieldValue.includes(option.exportValue)) {
  814. optionElement.setAttribute("selected", true);
  815. }
  816. selectElement.appendChild(optionElement);
  817. }
  818. const getValue = (event, isExport) => {
  819. const name = isExport ? "value" : "textContent";
  820. const options = event.target.options;
  821. if (!event.target.multiple) {
  822. return options.selectedIndex === -1 ? null : options[options.selectedIndex][name];
  823. }
  824. return Array.prototype.filter.call(options, option => option.selected).map(option => option[name]);
  825. };
  826. const getItems = event => {
  827. const options = event.target.options;
  828. return Array.prototype.map.call(options, option => {
  829. return {
  830. displayValue: option.textContent,
  831. exportValue: option.value
  832. };
  833. });
  834. };
  835. if (this.enableScripting && this.hasJSActions) {
  836. selectElement.addEventListener("updatefromsandbox", event => {
  837. const {
  838. detail
  839. } = event;
  840. const actions = {
  841. value() {
  842. const options = selectElement.options;
  843. const value = detail.value;
  844. const values = new Set(Array.isArray(value) ? value : [value]);
  845. Array.prototype.forEach.call(options, option => {
  846. option.selected = values.has(option.value);
  847. });
  848. storage.setValue(id, {
  849. value: getValue(event, true)
  850. });
  851. },
  852. multipleSelection() {
  853. selectElement.multiple = true;
  854. },
  855. remove() {
  856. const options = selectElement.options;
  857. const index = detail.remove;
  858. options[index].selected = false;
  859. selectElement.remove(index);
  860. if (options.length > 0) {
  861. const i = Array.prototype.findIndex.call(options, option => option.selected);
  862. if (i === -1) {
  863. options[0].selected = true;
  864. }
  865. }
  866. storage.setValue(id, {
  867. value: getValue(event, true),
  868. items: getItems(event)
  869. });
  870. },
  871. clear() {
  872. while (selectElement.length !== 0) {
  873. selectElement.remove(0);
  874. }
  875. storage.setValue(id, {
  876. value: null,
  877. items: []
  878. });
  879. },
  880. insert() {
  881. const {
  882. index,
  883. displayValue,
  884. exportValue
  885. } = detail.insert;
  886. const optionElement = document.createElement("option");
  887. optionElement.textContent = displayValue;
  888. optionElement.value = exportValue;
  889. selectElement.insertBefore(optionElement, selectElement.children[index]);
  890. storage.setValue(id, {
  891. value: getValue(event, true),
  892. items: getItems(event)
  893. });
  894. },
  895. items() {
  896. const {
  897. items
  898. } = detail;
  899. while (selectElement.length !== 0) {
  900. selectElement.remove(0);
  901. }
  902. for (const item of items) {
  903. const {
  904. displayValue,
  905. exportValue
  906. } = item;
  907. const optionElement = document.createElement("option");
  908. optionElement.textContent = displayValue;
  909. optionElement.value = exportValue;
  910. selectElement.appendChild(optionElement);
  911. }
  912. if (selectElement.options.length > 0) {
  913. selectElement.options[0].selected = true;
  914. }
  915. storage.setValue(id, {
  916. value: getValue(event, true),
  917. items: getItems(event)
  918. });
  919. },
  920. indices() {
  921. const indices = new Set(detail.indices);
  922. const options = event.target.options;
  923. Array.prototype.forEach.call(options, (option, i) => {
  924. option.selected = indices.has(i);
  925. });
  926. storage.setValue(id, {
  927. value: getValue(event, true)
  928. });
  929. },
  930. focus() {
  931. setTimeout(() => event.target.focus({
  932. preventScroll: false
  933. }), 0);
  934. },
  935. hidden() {
  936. event.target.style.visibility = detail.hidden ? "hidden" : "visible";
  937. storage.setValue(id, {
  938. hidden: detail.hidden
  939. });
  940. },
  941. editable() {
  942. event.target.disabled = !detail.editable;
  943. }
  944. };
  945. Object.keys(detail).filter(name => name in actions).forEach(name => actions[name]());
  946. this._setColor(event);
  947. });
  948. selectElement.addEventListener("input", event => {
  949. const exportValue = getValue(event, true);
  950. const value = getValue(event, false);
  951. storage.setValue(id, {
  952. value: exportValue
  953. });
  954. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  955. source: this,
  956. detail: {
  957. id,
  958. name: "Keystroke",
  959. value,
  960. changeEx: exportValue,
  961. willCommit: true,
  962. commitKey: 1,
  963. keyDown: false
  964. }
  965. });
  966. });
  967. 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);
  968. } else {
  969. selectElement.addEventListener("input", function (event) {
  970. storage.setValue(id, {
  971. value: getValue(event)
  972. });
  973. });
  974. }
  975. this.container.appendChild(selectElement);
  976. return this.container;
  977. }
  978. }
  979. class PopupAnnotationElement extends AnnotationElement {
  980. constructor(parameters) {
  981. const isRenderable = !!(parameters.data.title || parameters.data.contents);
  982. super(parameters, {
  983. isRenderable
  984. });
  985. }
  986. render() {
  987. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  988. this.container.className = "popupAnnotation";
  989. if (IGNORE_TYPES.includes(this.data.parentType)) {
  990. return this.container;
  991. }
  992. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  993. const parentElements = this.layer.querySelectorAll(selector);
  994. if (parentElements.length === 0) {
  995. return this.container;
  996. }
  997. const popup = new PopupElement({
  998. container: this.container,
  999. trigger: Array.from(parentElements),
  1000. color: this.data.color,
  1001. title: this.data.title,
  1002. modificationDate: this.data.modificationDate,
  1003. contents: this.data.contents
  1004. });
  1005. const page = this.page;
  1006. 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]]);
  1007. const popupLeft = rect[0] + this.data.parentRect[2] - this.data.parentRect[0];
  1008. const popupTop = rect[1];
  1009. this.container.style.transformOrigin = `${-popupLeft}px ${-popupTop}px`;
  1010. this.container.style.left = `${popupLeft}px`;
  1011. this.container.style.top = `${popupTop}px`;
  1012. this.container.appendChild(popup.render());
  1013. return this.container;
  1014. }
  1015. }
  1016. class PopupElement {
  1017. constructor(parameters) {
  1018. this.container = parameters.container;
  1019. this.trigger = parameters.trigger;
  1020. this.color = parameters.color;
  1021. this.title = parameters.title;
  1022. this.modificationDate = parameters.modificationDate;
  1023. this.contents = parameters.contents;
  1024. this.hideWrapper = parameters.hideWrapper || false;
  1025. this.pinned = false;
  1026. }
  1027. render() {
  1028. const BACKGROUND_ENLIGHT = 0.7;
  1029. const wrapper = document.createElement("div");
  1030. wrapper.className = "popupWrapper";
  1031. this.hideElement = this.hideWrapper ? wrapper : this.container;
  1032. this.hideElement.hidden = true;
  1033. const popup = document.createElement("div");
  1034. popup.className = "popup";
  1035. const color = this.color;
  1036. if (color) {
  1037. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  1038. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  1039. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  1040. popup.style.backgroundColor = _util.Util.makeHexColor(r | 0, g | 0, b | 0);
  1041. }
  1042. const title = document.createElement("h1");
  1043. title.textContent = this.title;
  1044. popup.appendChild(title);
  1045. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  1046. if (dateObject) {
  1047. const modificationDate = document.createElement("span");
  1048. modificationDate.textContent = "{{date}}, {{time}}";
  1049. modificationDate.dataset.l10nId = "annotation_date_string";
  1050. modificationDate.dataset.l10nArgs = JSON.stringify({
  1051. date: dateObject.toLocaleDateString(),
  1052. time: dateObject.toLocaleTimeString()
  1053. });
  1054. popup.appendChild(modificationDate);
  1055. }
  1056. const contents = this._formatContents(this.contents);
  1057. popup.appendChild(contents);
  1058. if (!Array.isArray(this.trigger)) {
  1059. this.trigger = [this.trigger];
  1060. }
  1061. this.trigger.forEach(element => {
  1062. element.addEventListener("click", this._toggle.bind(this));
  1063. element.addEventListener("mouseover", this._show.bind(this, false));
  1064. element.addEventListener("mouseout", this._hide.bind(this, false));
  1065. });
  1066. popup.addEventListener("click", this._hide.bind(this, true));
  1067. wrapper.appendChild(popup);
  1068. return wrapper;
  1069. }
  1070. _formatContents(contents) {
  1071. const p = document.createElement("p");
  1072. const lines = contents.split(/(?:\r\n?|\n)/);
  1073. for (let i = 0, ii = lines.length; i < ii; ++i) {
  1074. const line = lines[i];
  1075. p.appendChild(document.createTextNode(line));
  1076. if (i < ii - 1) {
  1077. p.appendChild(document.createElement("br"));
  1078. }
  1079. }
  1080. return p;
  1081. }
  1082. _toggle() {
  1083. if (this.pinned) {
  1084. this._hide(true);
  1085. } else {
  1086. this._show(true);
  1087. }
  1088. }
  1089. _show(pin = false) {
  1090. if (pin) {
  1091. this.pinned = true;
  1092. }
  1093. if (this.hideElement.hidden) {
  1094. this.hideElement.hidden = false;
  1095. this.container.style.zIndex += 1;
  1096. }
  1097. }
  1098. _hide(unpin = true) {
  1099. if (unpin) {
  1100. this.pinned = false;
  1101. }
  1102. if (!this.hideElement.hidden && !this.pinned) {
  1103. this.hideElement.hidden = true;
  1104. this.container.style.zIndex -= 1;
  1105. }
  1106. }
  1107. }
  1108. class FreeTextAnnotationElement extends AnnotationElement {
  1109. constructor(parameters) {
  1110. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1111. super(parameters, {
  1112. isRenderable,
  1113. ignoreBorder: true
  1114. });
  1115. }
  1116. render() {
  1117. this.container.className = "freeTextAnnotation";
  1118. if (!this.data.hasPopup) {
  1119. this._createPopup(null, this.data);
  1120. }
  1121. return this.container;
  1122. }
  1123. }
  1124. class LineAnnotationElement extends AnnotationElement {
  1125. constructor(parameters) {
  1126. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1127. super(parameters, {
  1128. isRenderable,
  1129. ignoreBorder: true
  1130. });
  1131. }
  1132. render() {
  1133. this.container.className = "lineAnnotation";
  1134. const data = this.data;
  1135. const width = data.rect[2] - data.rect[0];
  1136. const height = data.rect[3] - data.rect[1];
  1137. const svg = this.svgFactory.create(width, height);
  1138. const line = this.svgFactory.createElement("svg:line");
  1139. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  1140. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  1141. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  1142. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  1143. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  1144. line.setAttribute("stroke", "transparent");
  1145. svg.appendChild(line);
  1146. this.container.append(svg);
  1147. this._createPopup(line, data);
  1148. return this.container;
  1149. }
  1150. }
  1151. class SquareAnnotationElement extends AnnotationElement {
  1152. constructor(parameters) {
  1153. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1154. super(parameters, {
  1155. isRenderable,
  1156. ignoreBorder: true
  1157. });
  1158. }
  1159. render() {
  1160. this.container.className = "squareAnnotation";
  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. const borderWidth = data.borderStyle.width;
  1166. const square = this.svgFactory.createElement("svg:rect");
  1167. square.setAttribute("x", borderWidth / 2);
  1168. square.setAttribute("y", borderWidth / 2);
  1169. square.setAttribute("width", width - borderWidth);
  1170. square.setAttribute("height", height - borderWidth);
  1171. square.setAttribute("stroke-width", borderWidth || 1);
  1172. square.setAttribute("stroke", "transparent");
  1173. square.setAttribute("fill", "none");
  1174. svg.appendChild(square);
  1175. this.container.append(svg);
  1176. this._createPopup(square, data);
  1177. return this.container;
  1178. }
  1179. }
  1180. class CircleAnnotationElement extends AnnotationElement {
  1181. constructor(parameters) {
  1182. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1183. super(parameters, {
  1184. isRenderable,
  1185. ignoreBorder: true
  1186. });
  1187. }
  1188. render() {
  1189. this.container.className = "circleAnnotation";
  1190. const data = this.data;
  1191. const width = data.rect[2] - data.rect[0];
  1192. const height = data.rect[3] - data.rect[1];
  1193. const svg = this.svgFactory.create(width, height);
  1194. const borderWidth = data.borderStyle.width;
  1195. const circle = this.svgFactory.createElement("svg:ellipse");
  1196. circle.setAttribute("cx", width / 2);
  1197. circle.setAttribute("cy", height / 2);
  1198. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  1199. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  1200. circle.setAttribute("stroke-width", borderWidth || 1);
  1201. circle.setAttribute("stroke", "transparent");
  1202. circle.setAttribute("fill", "none");
  1203. svg.appendChild(circle);
  1204. this.container.append(svg);
  1205. this._createPopup(circle, data);
  1206. return this.container;
  1207. }
  1208. }
  1209. class PolylineAnnotationElement extends AnnotationElement {
  1210. constructor(parameters) {
  1211. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1212. super(parameters, {
  1213. isRenderable,
  1214. ignoreBorder: true
  1215. });
  1216. this.containerClassName = "polylineAnnotation";
  1217. this.svgElementName = "svg:polyline";
  1218. }
  1219. render() {
  1220. this.container.className = this.containerClassName;
  1221. const data = this.data;
  1222. const width = data.rect[2] - data.rect[0];
  1223. const height = data.rect[3] - data.rect[1];
  1224. const svg = this.svgFactory.create(width, height);
  1225. let points = [];
  1226. for (const coordinate of data.vertices) {
  1227. const x = coordinate.x - data.rect[0];
  1228. const y = data.rect[3] - coordinate.y;
  1229. points.push(x + "," + y);
  1230. }
  1231. points = points.join(" ");
  1232. const polyline = this.svgFactory.createElement(this.svgElementName);
  1233. polyline.setAttribute("points", points);
  1234. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1235. polyline.setAttribute("stroke", "transparent");
  1236. polyline.setAttribute("fill", "none");
  1237. svg.appendChild(polyline);
  1238. this.container.append(svg);
  1239. this._createPopup(polyline, data);
  1240. return this.container;
  1241. }
  1242. }
  1243. class PolygonAnnotationElement extends PolylineAnnotationElement {
  1244. constructor(parameters) {
  1245. super(parameters);
  1246. this.containerClassName = "polygonAnnotation";
  1247. this.svgElementName = "svg:polygon";
  1248. }
  1249. }
  1250. class CaretAnnotationElement extends AnnotationElement {
  1251. constructor(parameters) {
  1252. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1253. super(parameters, {
  1254. isRenderable,
  1255. ignoreBorder: true
  1256. });
  1257. }
  1258. render() {
  1259. this.container.className = "caretAnnotation";
  1260. if (!this.data.hasPopup) {
  1261. this._createPopup(null, this.data);
  1262. }
  1263. return this.container;
  1264. }
  1265. }
  1266. class InkAnnotationElement extends AnnotationElement {
  1267. constructor(parameters) {
  1268. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1269. super(parameters, {
  1270. isRenderable,
  1271. ignoreBorder: true
  1272. });
  1273. this.containerClassName = "inkAnnotation";
  1274. this.svgElementName = "svg:polyline";
  1275. }
  1276. render() {
  1277. this.container.className = this.containerClassName;
  1278. const data = this.data;
  1279. const width = data.rect[2] - data.rect[0];
  1280. const height = data.rect[3] - data.rect[1];
  1281. const svg = this.svgFactory.create(width, height);
  1282. for (const inkList of data.inkLists) {
  1283. let points = [];
  1284. for (const coordinate of inkList) {
  1285. const x = coordinate.x - data.rect[0];
  1286. const y = data.rect[3] - coordinate.y;
  1287. points.push(`${x},${y}`);
  1288. }
  1289. points = points.join(" ");
  1290. const polyline = this.svgFactory.createElement(this.svgElementName);
  1291. polyline.setAttribute("points", points);
  1292. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1293. polyline.setAttribute("stroke", "transparent");
  1294. polyline.setAttribute("fill", "none");
  1295. this._createPopup(polyline, data);
  1296. svg.appendChild(polyline);
  1297. }
  1298. this.container.append(svg);
  1299. return this.container;
  1300. }
  1301. }
  1302. class HighlightAnnotationElement extends AnnotationElement {
  1303. constructor(parameters) {
  1304. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1305. super(parameters, {
  1306. isRenderable,
  1307. ignoreBorder: true,
  1308. createQuadrilaterals: true
  1309. });
  1310. }
  1311. render() {
  1312. if (!this.data.hasPopup) {
  1313. this._createPopup(null, this.data);
  1314. }
  1315. if (this.quadrilaterals) {
  1316. return this._renderQuadrilaterals("highlightAnnotation");
  1317. }
  1318. this.container.className = "highlightAnnotation";
  1319. return this.container;
  1320. }
  1321. }
  1322. class UnderlineAnnotationElement extends AnnotationElement {
  1323. constructor(parameters) {
  1324. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1325. super(parameters, {
  1326. isRenderable,
  1327. ignoreBorder: true,
  1328. createQuadrilaterals: true
  1329. });
  1330. }
  1331. render() {
  1332. if (!this.data.hasPopup) {
  1333. this._createPopup(null, this.data);
  1334. }
  1335. if (this.quadrilaterals) {
  1336. return this._renderQuadrilaterals("underlineAnnotation");
  1337. }
  1338. this.container.className = "underlineAnnotation";
  1339. return this.container;
  1340. }
  1341. }
  1342. class SquigglyAnnotationElement extends AnnotationElement {
  1343. constructor(parameters) {
  1344. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1345. super(parameters, {
  1346. isRenderable,
  1347. ignoreBorder: true,
  1348. createQuadrilaterals: true
  1349. });
  1350. }
  1351. render() {
  1352. if (!this.data.hasPopup) {
  1353. this._createPopup(null, this.data);
  1354. }
  1355. if (this.quadrilaterals) {
  1356. return this._renderQuadrilaterals("squigglyAnnotation");
  1357. }
  1358. this.container.className = "squigglyAnnotation";
  1359. return this.container;
  1360. }
  1361. }
  1362. class StrikeOutAnnotationElement extends AnnotationElement {
  1363. constructor(parameters) {
  1364. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1365. super(parameters, {
  1366. isRenderable,
  1367. ignoreBorder: true,
  1368. createQuadrilaterals: true
  1369. });
  1370. }
  1371. render() {
  1372. if (!this.data.hasPopup) {
  1373. this._createPopup(null, this.data);
  1374. }
  1375. if (this.quadrilaterals) {
  1376. return this._renderQuadrilaterals("strikeoutAnnotation");
  1377. }
  1378. this.container.className = "strikeoutAnnotation";
  1379. return this.container;
  1380. }
  1381. }
  1382. class StampAnnotationElement extends AnnotationElement {
  1383. constructor(parameters) {
  1384. const isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  1385. super(parameters, {
  1386. isRenderable,
  1387. ignoreBorder: true
  1388. });
  1389. }
  1390. render() {
  1391. this.container.className = "stampAnnotation";
  1392. if (!this.data.hasPopup) {
  1393. this._createPopup(null, this.data);
  1394. }
  1395. return this.container;
  1396. }
  1397. }
  1398. class FileAttachmentAnnotationElement extends AnnotationElement {
  1399. constructor(parameters) {
  1400. super(parameters, {
  1401. isRenderable: true
  1402. });
  1403. const {
  1404. filename,
  1405. content
  1406. } = this.data.file;
  1407. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  1408. this.content = content;
  1409. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  1410. source: this,
  1411. id: (0, _util.stringToPDFString)(filename),
  1412. filename,
  1413. content
  1414. });
  1415. }
  1416. render() {
  1417. this.container.className = "fileAttachmentAnnotation";
  1418. const trigger = document.createElement("div");
  1419. trigger.style.height = this.container.style.height;
  1420. trigger.style.width = this.container.style.width;
  1421. trigger.addEventListener("dblclick", this._download.bind(this));
  1422. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  1423. this._createPopup(trigger, this.data);
  1424. }
  1425. this.container.appendChild(trigger);
  1426. return this.container;
  1427. }
  1428. _download() {
  1429. this.downloadManager?.openOrDownloadData(this.container, this.content, this.filename);
  1430. }
  1431. }
  1432. class AnnotationLayer {
  1433. static render(parameters) {
  1434. const sortedAnnotations = [],
  1435. popupAnnotations = [];
  1436. for (const data of parameters.annotations) {
  1437. if (!data) {
  1438. continue;
  1439. }
  1440. if (data.annotationType === _util.AnnotationType.POPUP) {
  1441. popupAnnotations.push(data);
  1442. continue;
  1443. }
  1444. sortedAnnotations.push(data);
  1445. }
  1446. if (popupAnnotations.length) {
  1447. sortedAnnotations.push(...popupAnnotations);
  1448. }
  1449. for (const data of sortedAnnotations) {
  1450. const element = AnnotationElementFactory.create({
  1451. data,
  1452. layer: parameters.div,
  1453. page: parameters.page,
  1454. viewport: parameters.viewport,
  1455. linkService: parameters.linkService,
  1456. downloadManager: parameters.downloadManager,
  1457. imageResourcesPath: parameters.imageResourcesPath || "",
  1458. renderInteractiveForms: parameters.renderInteractiveForms !== false,
  1459. svgFactory: new _display_utils.DOMSVGFactory(),
  1460. annotationStorage: parameters.annotationStorage || new _annotation_storage.AnnotationStorage(),
  1461. enableScripting: parameters.enableScripting,
  1462. hasJSActions: parameters.hasJSActions,
  1463. mouseState: parameters.mouseState || {
  1464. isDown: false
  1465. }
  1466. });
  1467. if (element.isRenderable) {
  1468. const rendered = element.render();
  1469. if (data.hidden) {
  1470. rendered.style.visibility = "hidden";
  1471. }
  1472. if (Array.isArray(rendered)) {
  1473. for (const renderedElement of rendered) {
  1474. parameters.div.appendChild(renderedElement);
  1475. }
  1476. } else {
  1477. if (element instanceof PopupAnnotationElement) {
  1478. parameters.div.prepend(rendered);
  1479. } else {
  1480. parameters.div.appendChild(rendered);
  1481. }
  1482. }
  1483. }
  1484. }
  1485. }
  1486. static update(parameters) {
  1487. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  1488. for (const data of parameters.annotations) {
  1489. const elements = parameters.div.querySelectorAll(`[data-annotation-id="${data.id}"]`);
  1490. if (elements) {
  1491. elements.forEach(element => {
  1492. element.style.transform = transform;
  1493. });
  1494. }
  1495. }
  1496. parameters.div.hidden = false;
  1497. }
  1498. }
  1499. exports.AnnotationLayer = AnnotationLayer;