summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/parent/ext-composeAction.js
blob: fb2a462d337b4d61dd24287dad780d687726c178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

ChromeUtils.defineModuleGetter(
  this,
  "ToolbarButtonAPI",
  "resource:///modules/ExtensionToolbarButtons.jsm"
);

const composeActionMap = new WeakMap();

var { ExtensionCommon } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionCommon.sys.mjs"
);
var { makeWidgetId } = ExtensionCommon;

this.composeAction = class extends ToolbarButtonAPI {
  static for(extension) {
    return composeActionMap.get(extension);
  }

  async onManifestEntry(entryName) {
    await super.onManifestEntry(entryName);
    composeActionMap.set(this.extension, this);
  }

  close() {
    super.close();
    composeActionMap.delete(this.extension);
  }

  constructor(extension) {
    super(extension, global);
    this.manifest_name = "compose_action";
    this.manifestName = "composeAction";
    this.manifest = extension.manifest[this.manifest_name];
    this.moduleName = this.manifestName;

    this.windowURLs = [
      "chrome://messenger/content/messengercompose/messengercompose.xhtml",
    ];
    let isFormatToolbar =
      extension.manifest.compose_action.default_area == "formattoolbar";
    this.toolboxId = isFormatToolbar ? "FormatToolbox" : "compose-toolbox";
    this.toolbarId = isFormatToolbar ? "FormatToolbar" : "composeToolbar2";
  }

  static onUninstall(extensionId) {
    let widgetId = makeWidgetId(extensionId);
    let id = `${widgetId}-composeAction-toolbarbutton`;
    let windowURL =
      "chrome://messenger/content/messengercompose/messengercompose.xhtml";

    // Check all possible toolbars and remove the toolbarbutton if found.
    // Sadly we have to hardcode these values here, as the add-on is already
    // shutdown when onUninstall is called.
    let toolbars = ["composeToolbar2", "FormatToolbar"];
    for (let toolbar of toolbars) {
      for (let setName of ["currentset", "extensionset"]) {
        let set = Services.xulStore
          .getValue(windowURL, toolbar, setName)
          .split(",");
        let newSet = set.filter(e => e != id);
        if (newSet.length < set.length) {
          Services.xulStore.setValue(
            windowURL,
            toolbar,
            setName,
            newSet.join(",")
          );
        }
      }
    }
  }

  handleEvent(event) {
    super.handleEvent(event);
    let window = event.target.ownerGlobal;

    switch (event.type) {
      case "popupshowing":
        const menu = event.target;
        if (menu.tagName != "menupopup") {
          return;
        }

        const trigger = menu.triggerNode;
        const node = window.document.getElementById(this.id);
        const contexts = [
          "format-toolbar-context-menu",
          "toolbar-context-menu",
          "customizationPanelItemContextMenu",
        ];
        if (contexts.includes(menu.id) && node && node.contains(trigger)) {
          global.actionContextMenu({
            tab: window,
            pageUrl: window.browser.currentURI.spec,
            extension: this.extension,
            onComposeAction: true,
            menu,
          });
        }

        if (
          menu.dataset.actionMenu == "composeAction" &&
          this.extension.id == menu.dataset.extensionId
        ) {
          global.actionContextMenu({
            tab: window,
            pageUrl: window.browser.currentURI.spec,
            extension: this.extension,
            inComposeActionMenu: true,
            menu,
          });
        }
        break;
    }
  }

  makeButton(window) {
    let button = super.makeButton(window);
    if (this.toolbarId == "FormatToolbar") {
      button.classList.add("formatting-button");
      // The format toolbar has no associated context menu. Add one directly to
      // this button.
      button.setAttribute("context", "format-toolbar-context-menu");
    }
    return button;
  }

  /**
   * Returns an element in the toolbar, which is to be used as default insertion
   * point for new toolbar buttons in non-customizable toolbars.
   *
   * May return null to append new buttons to the end of the toolbar.
   *
   * @param {DOMElement} toolbar - a toolbar node
   * @returns {DOMElement} a node which is to be used as insertion point, or null
   */
  getNonCustomizableToolbarInsertionPoint(toolbar) {
    let before = toolbar.lastElementChild;
    while (before.localName == "spacer") {
      before = before.previousElementSibling;
    }
    return before.nextElementSibling;
  }
};

global.composeActionFor = this.composeAction.for;