summaryrefslogtreecommitdiffstats
path: root/comm/suite/editor/base/content/editingOverlay.js
blob: 19b41d321bd1e5f084bcaa0a66dd3ab5e88305d0 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

var gUntitledString;

function TextEditorOnLoad()
{
  var url = "about:blank";
  // See if argument was passed.
  if (window.arguments && window.arguments[0])
  {
    // Opened via window.openDialog with URL as argument.
    url = window.arguments[0];
  }
  // Continue with normal startup.
  EditorStartup(url);
}

function EditorOnLoad()
{
  var url = "about:blank";
  var charset;
  // See if argument was passed.
  if (window.arguments)
  {
    if (window.arguments[0])
    {
      // Opened via window.openDialog with URL as argument.
      url = window.arguments[0];
    }

    // get default character set if provided
    if (window.arguments.length > 1 && window.arguments[1])
    {
      if (window.arguments[1].includes("charset="))
      {
        var arrayArgComponents = window.arguments[1].split("=");
        if (arrayArgComponents)
          charset = arrayArgComponents[1];
      }
    }
  }

  // XUL elements we use when switching from normal editor to edit source.
  gContentWindowDeck = document.getElementById("ContentWindowDeck");
  gFormatToolbar = document.getElementById("FormatToolbar");

  // Continue with normal startup.
  EditorStartup(url, charset);

  // Hide Highlight button if we are in an HTML editor with CSS mode off
  // and tell the editor if a CR in a paragraph creates a new paragraph.
  var cmd = document.getElementById("cmd_highlight");
  if (cmd) {
    if (!Services.prefs.getBoolPref(kUseCssPref))
      cmd.collapsed = true;
  }

  // Initialize our source text <editor>
  try {
    gSourceContentWindow = document.getElementById("content-source");
    gSourceContentWindow.makeEditable("text", false);
    gSourceTextEditor = gSourceContentWindow.getEditor(gSourceContentWindow.contentWindow);
    gSourceTextEditor.enableUndo(false);
    gSourceTextEditor.rootElement.style.fontFamily = "-moz-fixed";
    gSourceTextEditor.rootElement.style.whiteSpace = "pre";
    gSourceTextEditor.rootElement.style.margin = 0;
    var controller = Cc["@mozilla.org/embedcomp/base-command-controller;1"]
                       .createInstance(Ci.nsIControllerContext);
    controller.setCommandContext(gSourceContentWindow);
    gSourceContentWindow.contentWindow.controllers.insertControllerAt(0, controller);
    var commandTable = controller.QueryInterface(Ci.nsIInterfaceRequestor)
                                 .getInterface(Ci.nsIControllerCommandTable);
    commandTable.registerCommand("cmd_findReplace", nsFindReplaceCommand);
    commandTable.registerCommand("cmd_find",        nsFindCommand);
    commandTable.registerCommand("cmd_findNext",    nsFindAgainCommand);
    commandTable.registerCommand("cmd_findPrev",    nsFindAgainCommand);
  } catch (e) {
    dump("makeEditable failed: "+e+"\n");
  }
}

function toggleAffectedChrome(aHide)
{
  // chrome to toggle includes:
  //   (*) menubar
  //   (*) toolbox
  //   (*) sidebar
  //   (*) statusbar

  if (!gChromeState)
    gChromeState = new Object;

  var statusbar = document.getElementById("status-bar");

  // sidebar states map as follows:
  //   hidden    => hide/show nothing
  //   collapsed => hide/show only the splitter
  //   shown     => hide/show the splitter and the box
  if (aHide)
  {
    // going into print preview mode
    gChromeState.sidebar = SidebarGetState();
    SidebarSetState("hidden");

    // deal with the Status Bar
    gChromeState.statusbarWasHidden = statusbar.hidden;
    statusbar.hidden = true;
  }
  else
  {
    // restoring normal mode (i.e., leaving print preview mode)
    SidebarSetState(gChromeState.sidebar);

    // restore the Status Bar
    statusbar.hidden = gChromeState.statusbarWasHidden;
  }

  // if we are unhiding and sidebar used to be there rebuild it
  if (!aHide && gChromeState.sidebar == "visible")
    SidebarRebuild();

  document.getElementById("EditorToolbox").hidden = aHide;
  document.getElementById("appcontent").collapsed = aHide;
}

var PrintPreviewListener = {
  getPrintPreviewBrowser: function () {
    var browser = document.getElementById("ppBrowser");
    if (!browser) {
      browser = document.createXULElement("browser");
      browser.setAttribute("id", "ppBrowser");
      browser.setAttribute("flex", "1");
      browser.setAttribute("disablehistory", "true");
      browser.setAttribute("disablesecurity", "true");
      browser.setAttribute("type", "content");
      document.getElementById("sidebar-parent").
        insertBefore(browser, document.getElementById("appcontent"));
    }
    return browser;
  },
  getSourceBrowser: function () {
    return GetCurrentEditorElement();
  },
  getNavToolbox: function () {
    return document.getElementById("EditorToolbox");
  },
  onEnter: function () {
    toggleAffectedChrome(true);
  },
  onExit: function () {
    document.getElementById("ppBrowser").collapsed = true;
    toggleAffectedChrome(false);
  }
}

function EditorStartup(aUrl, aCharset)
{
  gUntitledString = GetFormattedString("untitledTitle", GetNextUntitledValue());

  var ds = GetCurrentEditorElement().docShell;
  ds.useErrorPages = false;
  var root = ds.QueryInterface(Ci.nsIDocShellTreeItem)
               .rootTreeItem.QueryInterface(Ci.nsIDocShell);

  root.QueryInterface(Ci.nsIDocShell).appType =
    Ci.nsIDocShell.APP_TYPE_EDITOR;

  // EditorSharedStartup also used by Message Composer.
  EditorSharedStartup();

  // Commands specific to the Composer Application window,
  //  (i.e., not embedded editors)
  //  such as file-related commands, HTML Source editing, Edit Modes...
  SetupComposerWindowCommands();

  gCSSPrefListener = new nsPrefListener(kUseCssPref);
  gReturnInParagraphPrefListener = new nsPrefListener(kCRInParagraphsPref);
  Services.obs.addObserver(EditorCanClose, "quit-application-requested");

  root.charset = aCharset;

  // Get url for editor content and load it. The editor gets instantiated by
  // the editingSession when the URL has finished loading.
  EditorLoadUrl(aUrl);

  // Before and after callbacks for the customizeToolbar code.
  var editorToolbox = getEditorToolbox();
  editorToolbox.customizeInit = EditorToolboxCustomizeInit;
  editorToolbox.customizeDone = EditorToolboxCustomizeDone;
  editorToolbox.customizeChange = EditorToolboxCustomizeChange;
}

function EditorShutdown()
{
  Services.obs.removeObserver(EditorCanClose, "quit-application-requested");

  gCSSPrefListener.shutdown();
  gReturnInParagraphPrefListener.shutdown();

  try
  {
    var commandManager = GetCurrentCommandManager();
    commandManager.removeCommandObserver(gEditorDocumentObserver,
                                         "obs_documentCreated");
    commandManager.removeCommandObserver(gEditorDocumentObserver,
                                         "obs_documentWillBeDestroyed");
    commandManager.removeCommandObserver(gEditorDocumentObserver,
                                         "obs_documentLocationChanged");
  } catch (e) { dump (e); }
}

// --------------------------- File menu ---------------------------

// Check for changes to document and allow saving before closing
// This is hooked up to the OS's window close widget (e.g., "X" for Windows)
async function EditorCanClose(aCancelQuit, aTopic, aData)
{
  if (aTopic == "quit-application-requested" &&
      aCancelQuit instanceof Ci.nsISupportsPRBool &&
      aCancelQuit.data)
    return false;

  // Returns FALSE only if user cancels save action

  // "true" means allow "Don't Save" button
  var canClose = await CheckAndSaveDocument("cmd_close", true);

  // This is our only hook into closing via the "X" in the caption
  //   or "Quit" (or other paths?)
  //   so we must shift association to another
  //   editor or close any non-modal windows now
  if (canClose && "InsertCharWindow" in window && window.InsertCharWindow)
    SwitchInsertCharToAnotherEditorOrClose();

  if (!canClose && aTopic == "quit-application-requested")
    aCancelQuit.data = true;

  return canClose;
}

function BuildRecentPagesMenu()
{
  var editor = GetCurrentEditor();
  if (!editor)
    return;

  var popup = document.getElementById("menupopup_RecentFiles");
  if (!popup || !editor.document)
    return;

  // Delete existing menu
  while (popup.hasChildNodes())
    popup.lastChild.remove();

  // Current page is the "0" item in the list we save in prefs,
  //  but we don't include it in the menu.
  var curUrl = StripPassword(GetDocumentUrl());
  var historyCount = Services.prefs.getIntPref("editor.history.url_maximum", 10);

  var menuIndex = 1;
  for (var i = 0; i < historyCount; i++)
  {
    var url = Services.prefs.getStringPref("editor.history_url_" + i, "");

    // Skip over current url
    if (url && url != curUrl)
    {
      // Build the menu
      var title = Services.prefs.getStringPref("editor.history_title_" + i, "");
      var fileType = Services.prefs.getStringPref("editor.history_type_" + i, "");
      AppendRecentMenuitem(popup, title, url, fileType, menuIndex);
      menuIndex++;
    }
  }
}

function AppendRecentMenuitem(aPopup, aTitle, aUrl, aFileType, aIndex)
{
  if (!aPopup)
    return;

  var menuItem = document.createXULElement("menuitem");
  if (!menuItem)
    return;

  var accessKey = aIndex <= 10 ? String(aIndex % 10) : " ";

  // Show "title [url]" or just the URL.
  var itemString = aTitle ? aTitle + " [" + aUrl + "]" : aUrl;

  menuItem.setAttribute("label", accessKey + " " + itemString);
  menuItem.setAttribute("crop", "center");
  menuItem.setAttribute("tooltiptext", aUrl);
  menuItem.setAttribute("value", aUrl);
  menuItem.setAttribute("fileType", aFileType);
  if (accessKey != " ")
    menuItem.setAttribute("accesskey", accessKey);
  aPopup.appendChild(menuItem);
}

function EditorInitFileMenu()
{
  // Disable "Save" menuitem when editing remote url. User should use "Save As"

  var docUrl = GetDocumentUrl();
  var scheme = GetScheme(docUrl);
  if (scheme && scheme != "file")
    SetElementEnabledById("menu_saveCmd", false);

  // Enable recent pages submenu if there are any history entries in prefs.
  var historyUrl = "";

  if (Services.prefs.getIntPref("editor.history.url_maximum", 10))
  {
    historyUrl = Services.prefs.getStringPref("editor.history_url_0", "");

    // See if there's more if current file is only entry in history list.
    if (historyUrl && historyUrl == docUrl)
      historyUrl = Services.prefs.getStringPref("editor.history_url_1", "");
  }
  SetElementEnabledById("menu_RecentFiles", historyUrl != "");
}

function EditorUpdateCharsetMenu(aMenuPopup)
{
  if (IsDocumentModified() && !IsDocumentEmpty())
  {
    for (var i = 0; i < aMenuPopup.childNodes.length; i++)
      aMenuPopup.childNodes[i].setAttribute("disabled", "true");
  }

  UpdateCharsetMenu(content.document.characterSet, aMenuPopup);
}

// Zoom support.
function getBrowser()
{
  return IsInHTMLSourceMode() ? gSourceContentWindow : GetCurrentEditorElement();
}

// override the site-specific zoom object in viewZoomOverlay.js
var FullZoom = {
  init: function() {},
  reduce: function() { ZoomManager.reduce(); },
  enlarge: function() { ZoomManager.enlarge(); },
  zoom: function(aZoomValue) { ZoomManager.zoom = aZoomValue; },
  reset: function() { ZoomManager.zoom = 1; },
  setOther: function() { openZoomDialog(); }
};

function hideEditorUI(aHide) {
  for (let id of ["EditModeToolbar", "content-source", "content-frame"]) {
    let element = document.getElementById(id);
    if (!element)
      continue;

    if (aHide) {
      element.setAttribute("moz-collapsed", true);
    } else {
      element.removeAttribute("moz-collapsed");
    }
  }
}

function getEditorToolbox() {
  return document.getElementById("EditorToolbox");
}

function EditorToolboxCustomizeInit() {
  if (document.commandDispatcher.focusedWindow == content)
    window.focus();
  hideEditorUI(true);
  toolboxCustomizeInit("main-menubar");
}

function EditorToolboxCustomizeDone(aToolboxChanged) {
  toolboxCustomizeDone("main-menubar", getEditorToolbox(), aToolboxChanged);
  hideEditorUI(false);
  gContentWindow.focus();
}

function EditorToolboxCustomizeChange(aEvent) {
  toolboxCustomizeChange(getEditorToolbox(), aEvent);
}