toolbar.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Toolbar = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _pdf = require("../pdf");
  29. const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading";
  30. class Toolbar {
  31. #wasLocalized = false;
  32. constructor(options, eventBus, l10n) {
  33. this.toolbar = options.container;
  34. this.eventBus = eventBus;
  35. this.l10n = l10n;
  36. this.buttons = [{
  37. element: options.previous,
  38. eventName: "previouspage"
  39. }, {
  40. element: options.next,
  41. eventName: "nextpage"
  42. }, {
  43. element: options.zoomIn,
  44. eventName: "zoomin"
  45. }, {
  46. element: options.zoomOut,
  47. eventName: "zoomout"
  48. }, {
  49. element: options.print,
  50. eventName: "print"
  51. }, {
  52. element: options.download,
  53. eventName: "download"
  54. }, {
  55. element: options.editorFreeTextButton,
  56. eventName: "switchannotationeditormode",
  57. eventDetails: {
  58. get mode() {
  59. const {
  60. classList
  61. } = options.editorFreeTextButton;
  62. return classList.contains("toggled") ? _pdf.AnnotationEditorType.NONE : _pdf.AnnotationEditorType.FREETEXT;
  63. }
  64. }
  65. }, {
  66. element: options.editorInkButton,
  67. eventName: "switchannotationeditormode",
  68. eventDetails: {
  69. get mode() {
  70. const {
  71. classList
  72. } = options.editorInkButton;
  73. return classList.contains("toggled") ? _pdf.AnnotationEditorType.NONE : _pdf.AnnotationEditorType.INK;
  74. }
  75. }
  76. }];
  77. this.buttons.push({
  78. element: options.openFile,
  79. eventName: "openfile"
  80. });
  81. this.items = {
  82. numPages: options.numPages,
  83. pageNumber: options.pageNumber,
  84. scaleSelect: options.scaleSelect,
  85. customScaleOption: options.customScaleOption,
  86. previous: options.previous,
  87. next: options.next,
  88. zoomIn: options.zoomIn,
  89. zoomOut: options.zoomOut
  90. };
  91. this.#bindListeners(options);
  92. this.reset();
  93. }
  94. setPageNumber(pageNumber, pageLabel) {
  95. this.pageNumber = pageNumber;
  96. this.pageLabel = pageLabel;
  97. this.#updateUIState(false);
  98. }
  99. setPagesCount(pagesCount, hasPageLabels) {
  100. this.pagesCount = pagesCount;
  101. this.hasPageLabels = hasPageLabels;
  102. this.#updateUIState(true);
  103. }
  104. setPageScale(pageScaleValue, pageScale) {
  105. this.pageScaleValue = (pageScaleValue || pageScale).toString();
  106. this.pageScale = pageScale;
  107. this.#updateUIState(false);
  108. }
  109. reset() {
  110. this.pageNumber = 0;
  111. this.pageLabel = null;
  112. this.hasPageLabels = false;
  113. this.pagesCount = 0;
  114. this.pageScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  115. this.pageScale = _ui_utils.DEFAULT_SCALE;
  116. this.#updateUIState(true);
  117. this.updateLoadingIndicatorState();
  118. this.eventBus.dispatch("toolbarreset", {
  119. source: this
  120. });
  121. }
  122. #bindListeners(options) {
  123. const {
  124. pageNumber,
  125. scaleSelect
  126. } = this.items;
  127. const self = this;
  128. for (const {
  129. element,
  130. eventName,
  131. eventDetails
  132. } of this.buttons) {
  133. element.addEventListener("click", evt => {
  134. if (eventName !== null) {
  135. const details = {
  136. source: this
  137. };
  138. if (eventDetails) {
  139. for (const property in eventDetails) {
  140. details[property] = eventDetails[property];
  141. }
  142. }
  143. this.eventBus.dispatch(eventName, details);
  144. }
  145. });
  146. }
  147. pageNumber.addEventListener("click", function () {
  148. this.select();
  149. });
  150. pageNumber.addEventListener("change", function () {
  151. self.eventBus.dispatch("pagenumberchanged", {
  152. source: self,
  153. value: this.value
  154. });
  155. });
  156. scaleSelect.addEventListener("change", function () {
  157. if (this.value === "custom") {
  158. return;
  159. }
  160. self.eventBus.dispatch("scalechanged", {
  161. source: self,
  162. value: this.value
  163. });
  164. });
  165. scaleSelect.addEventListener("click", function (evt) {
  166. const target = evt.target;
  167. if (this.value === self.pageScaleValue && target.tagName.toUpperCase() === "OPTION") {
  168. this.blur();
  169. }
  170. });
  171. scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
  172. this.eventBus._on("localized", () => {
  173. this.#wasLocalized = true;
  174. this.#adjustScaleWidth();
  175. this.#updateUIState(true);
  176. });
  177. this.#bindEditorToolsListener(options);
  178. }
  179. #bindEditorToolsListener({
  180. editorFreeTextButton,
  181. editorFreeTextParamsToolbar,
  182. editorInkButton,
  183. editorInkParamsToolbar
  184. }) {
  185. const editorModeChanged = (evt, disableButtons = false) => {
  186. const editorButtons = [{
  187. mode: _pdf.AnnotationEditorType.FREETEXT,
  188. button: editorFreeTextButton,
  189. toolbar: editorFreeTextParamsToolbar
  190. }, {
  191. mode: _pdf.AnnotationEditorType.INK,
  192. button: editorInkButton,
  193. toolbar: editorInkParamsToolbar
  194. }];
  195. for (const {
  196. mode,
  197. button,
  198. toolbar
  199. } of editorButtons) {
  200. const checked = mode === evt.mode;
  201. button.classList.toggle("toggled", checked);
  202. button.setAttribute("aria-checked", checked);
  203. button.disabled = disableButtons;
  204. toolbar?.classList.toggle("hidden", !checked);
  205. }
  206. };
  207. this.eventBus._on("annotationeditormodechanged", editorModeChanged);
  208. this.eventBus._on("toolbarreset", evt => {
  209. if (evt.source === this) {
  210. editorModeChanged({
  211. mode: _pdf.AnnotationEditorType.NONE
  212. }, true);
  213. }
  214. });
  215. }
  216. #updateUIState(resetNumPages = false) {
  217. if (!this.#wasLocalized) {
  218. return;
  219. }
  220. const {
  221. pageNumber,
  222. pagesCount,
  223. pageScaleValue,
  224. pageScale,
  225. items
  226. } = this;
  227. if (resetNumPages) {
  228. if (this.hasPageLabels) {
  229. items.pageNumber.type = "text";
  230. } else {
  231. items.pageNumber.type = "number";
  232. this.l10n.get("of_pages", {
  233. pagesCount
  234. }).then(msg => {
  235. items.numPages.textContent = msg;
  236. });
  237. }
  238. items.pageNumber.max = pagesCount;
  239. }
  240. if (this.hasPageLabels) {
  241. items.pageNumber.value = this.pageLabel;
  242. this.l10n.get("page_of_pages", {
  243. pageNumber,
  244. pagesCount
  245. }).then(msg => {
  246. items.numPages.textContent = msg;
  247. });
  248. } else {
  249. items.pageNumber.value = pageNumber;
  250. }
  251. items.previous.disabled = pageNumber <= 1;
  252. items.next.disabled = pageNumber >= pagesCount;
  253. items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
  254. items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
  255. this.l10n.get("page_scale_percent", {
  256. scale: Math.round(pageScale * 10000) / 100
  257. }).then(msg => {
  258. let predefinedValueFound = false;
  259. for (const option of items.scaleSelect.options) {
  260. if (option.value !== pageScaleValue) {
  261. option.selected = false;
  262. continue;
  263. }
  264. option.selected = true;
  265. predefinedValueFound = true;
  266. }
  267. if (!predefinedValueFound) {
  268. items.customScaleOption.textContent = msg;
  269. items.customScaleOption.selected = true;
  270. }
  271. });
  272. }
  273. updateLoadingIndicatorState(loading = false) {
  274. const {
  275. pageNumber
  276. } = this.items;
  277. pageNumber.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
  278. }
  279. async #adjustScaleWidth() {
  280. const {
  281. items,
  282. l10n
  283. } = this;
  284. const predefinedValuesPromise = Promise.all([l10n.get("page_scale_auto"), l10n.get("page_scale_actual"), l10n.get("page_scale_fit"), l10n.get("page_scale_width")]);
  285. await _ui_utils.animationStarted;
  286. const style = getComputedStyle(items.scaleSelect);
  287. const scaleSelectWidth = parseFloat(style.getPropertyValue("--scale-select-width"));
  288. const canvas = document.createElement("canvas");
  289. const ctx = canvas.getContext("2d", {
  290. alpha: false
  291. });
  292. ctx.font = `${style.fontSize} ${style.fontFamily}`;
  293. let maxWidth = 0;
  294. for (const predefinedValue of await predefinedValuesPromise) {
  295. const {
  296. width
  297. } = ctx.measureText(predefinedValue);
  298. if (width > maxWidth) {
  299. maxWidth = width;
  300. }
  301. }
  302. maxWidth += 0.3 * scaleSelectWidth;
  303. if (maxWidth > scaleSelectWidth) {
  304. _ui_utils.docStyle.setProperty("--scale-select-width", `${maxWidth}px`);
  305. }
  306. canvas.width = 0;
  307. canvas.height = 0;
  308. }
  309. }
  310. exports.Toolbar = Toolbar;