1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.PDFSidebarResizer = void 0;
|
28 |
|
29 | var _ui_utils = require("./ui_utils.js");
|
30 |
|
31 | const SIDEBAR_WIDTH_VAR = "--sidebar-width";
|
32 | const SIDEBAR_MIN_WIDTH = 200;
|
33 | const SIDEBAR_RESIZING_CLASS = "sidebarResizing";
|
34 |
|
35 | class PDFSidebarResizer {
|
36 | constructor(options, eventBus, l10n) {
|
37 | this.isRTL = false;
|
38 | this.sidebarOpen = false;
|
39 | this._width = null;
|
40 | this._outerContainerWidth = null;
|
41 | this._boundEvents = Object.create(null);
|
42 | this.outerContainer = options.outerContainer;
|
43 | this.resizer = options.resizer;
|
44 | this.eventBus = eventBus;
|
45 | l10n.getDirection().then(dir => {
|
46 | this.isRTL = dir === "rtl";
|
47 | });
|
48 |
|
49 | this._addEventListeners();
|
50 | }
|
51 |
|
52 | get outerContainerWidth() {
|
53 | return this._outerContainerWidth ||= this.outerContainer.clientWidth;
|
54 | }
|
55 |
|
56 | _updateWidth(width = 0) {
|
57 | const maxWidth = Math.floor(this.outerContainerWidth / 2);
|
58 |
|
59 | if (width > maxWidth) {
|
60 | width = maxWidth;
|
61 | }
|
62 |
|
63 | if (width < SIDEBAR_MIN_WIDTH) {
|
64 | width = SIDEBAR_MIN_WIDTH;
|
65 | }
|
66 |
|
67 | if (width === this._width) {
|
68 | return false;
|
69 | }
|
70 |
|
71 | this._width = width;
|
72 |
|
73 | _ui_utils.docStyle.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
|
74 |
|
75 | return true;
|
76 | }
|
77 |
|
78 | _mouseMove(evt) {
|
79 | let width = evt.clientX;
|
80 |
|
81 | if (this.isRTL) {
|
82 | width = this.outerContainerWidth - width;
|
83 | }
|
84 |
|
85 | this._updateWidth(width);
|
86 | }
|
87 |
|
88 | _mouseUp(evt) {
|
89 | this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
|
90 | this.eventBus.dispatch("resize", {
|
91 | source: this
|
92 | });
|
93 | const _boundEvents = this._boundEvents;
|
94 | window.removeEventListener("mousemove", _boundEvents.mouseMove);
|
95 | window.removeEventListener("mouseup", _boundEvents.mouseUp);
|
96 | }
|
97 |
|
98 | _addEventListeners() {
|
99 | const _boundEvents = this._boundEvents;
|
100 | _boundEvents.mouseMove = this._mouseMove.bind(this);
|
101 | _boundEvents.mouseUp = this._mouseUp.bind(this);
|
102 | this.resizer.addEventListener("mousedown", evt => {
|
103 | if (evt.button !== 0) {
|
104 | return;
|
105 | }
|
106 |
|
107 | this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
|
108 | window.addEventListener("mousemove", _boundEvents.mouseMove);
|
109 | window.addEventListener("mouseup", _boundEvents.mouseUp);
|
110 | });
|
111 |
|
112 | this.eventBus._on("sidebarviewchanged", evt => {
|
113 | this.sidebarOpen = !!evt?.view;
|
114 | });
|
115 |
|
116 | this.eventBus._on("resize", evt => {
|
117 | if (evt?.source !== window) {
|
118 | return;
|
119 | }
|
120 |
|
121 | this._outerContainerWidth = null;
|
122 |
|
123 | if (!this._width) {
|
124 | return;
|
125 | }
|
126 |
|
127 | if (!this.sidebarOpen) {
|
128 | this._updateWidth(this._width);
|
129 |
|
130 | return;
|
131 | }
|
132 |
|
133 | this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
|
134 |
|
135 | const updated = this._updateWidth(this._width);
|
136 |
|
137 | Promise.resolve().then(() => {
|
138 | this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
|
139 |
|
140 | if (updated) {
|
141 | this.eventBus.dispatch("resize", {
|
142 | source: this
|
143 | });
|
144 | }
|
145 | });
|
146 | });
|
147 | }
|
148 |
|
149 | }
|
150 |
|
151 | exports.PDFSidebarResizer = PDFSidebarResizer; |
\ | No newline at end of file |