Skip to content

Commit 2548405

Browse files
committed
Slightly simplify the way to create the editor toolbar
1 parent 18d7aaf commit 2548405

File tree

6 files changed

+72
-44
lines changed

6 files changed

+72
-44
lines changed

src/display/editor/editor.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,14 @@ class AnnotationEditor {
10491049
this.#altText?.finish();
10501050
}
10511051

1052+
/**
1053+
* Get the toolbar buttons for this editor.
1054+
* @returns {Array<Array<string|object>>|null}
1055+
*/
1056+
get toolbarButtons() {
1057+
return null;
1058+
}
1059+
10521060
/**
10531061
* Add a toolbar for this editor.
10541062
* @returns {Promise<EditorToolbar|null>}
@@ -1059,9 +1067,13 @@ class AnnotationEditor {
10591067
}
10601068
this._editToolbar = new EditorToolbar(this);
10611069
this.div.append(this._editToolbar.render());
1062-
if (this.#altText) {
1063-
await this._editToolbar.addAltText(this.#altText);
1070+
const { toolbarButtons } = this;
1071+
if (toolbarButtons) {
1072+
for (const [name, tool] of toolbarButtons) {
1073+
await this._editToolbar.addButton(name, tool);
1074+
}
10641075
}
1076+
this._editToolbar.addButton("delete");
10651077

10661078
return this._editToolbar;
10671079
}
@@ -1091,17 +1103,20 @@ class AnnotationEditor {
10911103
return this.div.getBoundingClientRect();
10921104
}
10931105

1094-
async addAltTextButton() {
1095-
if (this.#altText) {
1096-
return;
1097-
}
1098-
AltText.initialize(AnnotationEditor._l10n);
1099-
this.#altText = new AltText(this);
1100-
if (this.#accessibilityData) {
1101-
this.#altText.data = this.#accessibilityData;
1102-
this.#accessibilityData = null;
1106+
/**
1107+
* Create the alt text for this editor.
1108+
* @returns {object}
1109+
*/
1110+
createAltText() {
1111+
if (!this.#altText) {
1112+
AltText.initialize(AnnotationEditor._l10n);
1113+
this.#altText = new AltText(this);
1114+
if (this.#accessibilityData) {
1115+
this.#altText.data = this.#accessibilityData;
1116+
this.#accessibilityData = null;
1117+
}
11031118
}
1104-
await this.addEditToolbar();
1119+
return this.#altText;
11051120
}
11061121

11071122
get altTextData() {

src/display/editor/highlight.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,14 @@ class HighlightEditor extends AnnotationEditor {
398398
}
399399

400400
/** @inheritdoc */
401-
async addEditToolbar() {
402-
const toolbar = await super.addEditToolbar();
403-
if (!toolbar) {
404-
return null;
405-
}
401+
get toolbarButtons() {
406402
if (this._uiManager.highlightColors) {
407-
this.#colorPicker = new ColorPicker({ editor: this });
408-
toolbar.addColorPicker(this.#colorPicker);
403+
const colorPicker = (this.#colorPicker = new ColorPicker({
404+
editor: this,
405+
}));
406+
return [["colorPicker", colorPicker]];
409407
}
410-
return toolbar;
408+
return super.toolbarButtons;
411409
}
412410

413411
/** @inheritdoc */

src/display/editor/signature.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,27 +244,18 @@ class SignatureEditor extends DrawingEditor {
244244
}
245245

246246
/** @inheritdoc */
247-
async addEditToolbar() {
248-
const toolbar = await super.addEditToolbar();
249-
if (!toolbar) {
250-
return null;
251-
}
252-
if (this._uiManager.signatureManager && this.#description !== null) {
253-
await toolbar.addEditSignatureButton(
254-
this._uiManager.signatureManager,
255-
this.#signatureUUID,
256-
this.#description
257-
);
258-
toolbar.show();
247+
get toolbarButtons() {
248+
if (this._uiManager.signatureManager) {
249+
return [["editSignature", this._uiManager.signatureManager]];
259250
}
260-
return toolbar;
251+
return super.toolbarButtons;
261252
}
262253

263254
addSignature(data, heightInPage, description, uuid) {
264255
const { x: savedX, y: savedY } = this;
265256
const { outline } = (this.#signatureData = data);
266257
this.#isExtracted = outline instanceof ContourDrawOutline;
267-
this.#description = description;
258+
this.description = description;
268259
this.div.setAttribute("data-l10n-args", JSON.stringify({ description }));
269260
let drawingOptions;
270261
if (this.#isExtracted) {

src/display/editor/stamp.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ class StampEditor extends AnnotationEditor {
128128
this._uiManager.useNewAltTextFlow &&
129129
this.#bitmap
130130
) {
131-
this._editToolbar.hide();
132-
this._uiManager.editAltText(this, /* firstTime = */ true);
131+
this.addEditToolbar().then(() => {
132+
this._editToolbar.hide();
133+
this._uiManager.editAltText(this, /* firstTime = */ true);
134+
});
133135
return;
134136
}
135137

@@ -336,6 +338,11 @@ class StampEditor extends AnnotationEditor {
336338
);
337339
}
338340

341+
/** @inheritdoc */
342+
get toolbarButtons() {
343+
return [["altText", this.createAltText()]];
344+
}
345+
339346
/** @inheritdoc */
340347
get isResizable() {
341348
return true;
@@ -356,7 +363,7 @@ class StampEditor extends AnnotationEditor {
356363
super.render();
357364
this.div.hidden = true;
358365

359-
this.addAltTextButton();
366+
this.createAltText();
360367

361368
if (!this.#missingCanvas) {
362369
if (this.#bitmap) {

src/display/editor/toolbar.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ class EditorToolbar {
6969
}% + var(--editor-toolbar-vert-offset))`;
7070
}
7171

72-
this.#addDeleteButton();
73-
7472
return editToolbar;
7573
}
7674

@@ -118,7 +116,7 @@ class EditorToolbar {
118116
this.#altText?.shown();
119117
}
120118

121-
#addDeleteButton() {
119+
addDeleteButton() {
122120
const { editorType, _uiManager } = this.#editor;
123121

124122
const button = document.createElement("button");
@@ -145,22 +143,39 @@ class EditorToolbar {
145143
async addAltText(altText) {
146144
const button = await altText.render();
147145
this.#addListenersToElement(button);
148-
this.#buttons.prepend(button, this.#divider);
146+
this.#buttons.append(button, this.#divider);
149147
this.#altText = altText;
150148
}
151149

152150
addColorPicker(colorPicker) {
153151
this.#colorPicker = colorPicker;
154152
const button = colorPicker.renderButton();
155153
this.#addListenersToElement(button);
156-
this.#buttons.prepend(button, this.#divider);
154+
this.#buttons.append(button, this.#divider);
157155
}
158156

159157
async addEditSignatureButton(signatureManager) {
160158
const button = (this.#signatureDescriptionButton =
161159
await signatureManager.renderEditButton(this.#editor));
162160
this.#addListenersToElement(button);
163-
this.#buttons.prepend(button, this.#divider);
161+
this.#buttons.append(button, this.#divider);
162+
}
163+
164+
async addButton(name, tool) {
165+
switch (name) {
166+
case "colorPicker":
167+
this.addColorPicker(tool);
168+
break;
169+
case "altText":
170+
await this.addAltText(tool);
171+
break;
172+
case "editSignature":
173+
await this.addEditSignatureButton(tool);
174+
break;
175+
case "delete":
176+
this.addDeleteButton();
177+
break;
178+
}
164179
}
165180

166181
updateEditSignatureButton(description) {

web/signature_manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,9 @@ class SignatureManager {
820820
const button = document.createElement("button");
821821
button.classList.add("altText", "editDescription");
822822
button.tabIndex = 0;
823-
button.title = editor.description;
823+
if (editor.description) {
824+
button.title = editor.description;
825+
}
824826
const span = document.createElement("span");
825827
button.append(span);
826828
span.setAttribute(

0 commit comments

Comments
 (0)