annotate static/js/pagedown/Markdown.Editor.js @ 60:8250c977bc50

Moved static files to the root directory.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Feb 2013 14:49:34 -0800
parents wikked/static/js/pagedown/Markdown.Editor.js@59cad6ce1a1c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 // needs Markdown.Converter.js at the moment
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 (function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 var util = {},
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 position = {},
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 ui = {},
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 doc = window.document,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 re = window.RegExp,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 nav = window.navigator,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 SETTINGS = { lineLength: 72 },
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 // Used to work around some browser bugs where we can't use feature testing.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 uaSniffed = {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 isIE: /msie/.test(nav.userAgent.toLowerCase()),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 isIE_5or6: /msie 6/.test(nav.userAgent.toLowerCase()) || /msie 5/.test(nav.userAgent.toLowerCase()),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 isOpera: /opera/.test(nav.userAgent.toLowerCase())
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 var defaultsStrings = {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 bold: "Strong <strong> Ctrl+B",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 boldexample: "strong text",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 italic: "Emphasis <em> Ctrl+I",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 italicexample: "emphasized text",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 link: "Hyperlink <a> Ctrl+L",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 linkdescription: "enter link description here",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 linkdialog: "<p><b>Insert Hyperlink</b></p><p>http://example.com/ \"optional title\"</p>",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 quote: "Blockquote <blockquote> Ctrl+Q",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 quoteexample: "Blockquote",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 code: "Code Sample <pre><code> Ctrl+K",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 codeexample: "enter code here",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 image: "Image <img> Ctrl+G",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 imagedescription: "enter image description here",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 imagedialog: "<p><b>Insert Image</b></p><p>http://example.com/images/diagram.jpg \"optional title\"<br><br>Need <a href='http://www.google.com/search?q=free+image+hosting' target='_blank'>free image hosting?</a></p>",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 olist: "Numbered List <ol> Ctrl+O",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 ulist: "Bulleted List <ul> Ctrl+U",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 litem: "List item",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 heading: "Heading <h1>/<h2> Ctrl+H",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 headingexample: "Heading",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 hr: "Horizontal Rule <hr> Ctrl+R",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 undo: "Undo - Ctrl+Z",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 redo: "Redo - Ctrl+Y",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 redomac: "Redo - Ctrl+Shift+Z",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 help: "Markdown Editing Help"
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 // -------------------------------------------------------------------
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 // YOUR CHANGES GO HERE
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 // I've tried to localize the things you are likely to change to
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 // this area.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 // -------------------------------------------------------------------
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 // The default text that appears in the dialog input box when entering
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 // links.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 var imageDefaultText = "http://";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 var linkDefaultText = "http://";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 // -------------------------------------------------------------------
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 // END OF YOUR CHANGES
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 // -------------------------------------------------------------------
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 // options, if given, can have the following properties:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 // options.helpButton = { handler: yourEventHandler }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 // options.strings = { italicexample: "slanted text" }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 // `yourEventHandler` is the click handler for the help button.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 // If `options.helpButton` isn't given, not help button is created.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 // `options.strings` can have any or all of the same properties as
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 // `defaultStrings` above, so you can just override some string displayed
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 // to the user on a case-by-case basis, or translate all strings to
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 // a different language.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 // For backwards compatibility reasons, the `options` argument can also
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 // be just the `helpButton` object, and `strings.help` can also be set via
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 // `helpButton.title`. This should be considered legacy.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 // The constructed editor object has the methods:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 // - getConverter() returns the markdown converter object that was passed to the constructor
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 // - run() actually starts the editor; should be called after all necessary plugins are registered. Calling this more than once is a no-op.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 // - refreshPreview() forces the preview to be updated. This method is only available after run() was called.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 Markdown.Editor = function (markdownConverter, idPostfix, options) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 options = options || {};
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 if (typeof options.handler === "function") { //backwards compatible behavior
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 options = { helpButton: options };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 options.strings = options.strings || {};
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 if (options.helpButton) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 options.strings.help = options.strings.help || options.helpButton.title;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 var getString = function (identifier) { return options.strings[identifier] || defaultsStrings[identifier]; }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 idPostfix = idPostfix || "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 var hooks = this.hooks = new Markdown.HookCollection();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 hooks.addNoop("onPreviewRefresh"); // called with no arguments after the preview has been refreshed
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 hooks.addNoop("postBlockquoteCreation"); // called with the user's selection *after* the blockquote was created; should return the actual to-be-inserted text
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 hooks.addFalse("insertImageDialog"); /* called with one parameter: a callback to be called with the URL of the image. If the application creates
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 * its own image insertion dialog, this hook should return true, and the callback should be called with the chosen
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 * image url (or null if the user cancelled). If this hook returns false, the default dialog will be used.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 */
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 this.getConverter = function () { return markdownConverter; }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 var that = this,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 panels;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 this.run = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 if (panels)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 return; // already initialized
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 panels = new PanelCollection(idPostfix);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 var commandManager = new CommandManager(hooks, getString);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 var previewManager = new PreviewManager(markdownConverter, panels, function () { hooks.onPreviewRefresh(); });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 var undoManager, uiManager;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 if (!/\?noundo/.test(doc.location.href)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 undoManager = new UndoManager(function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 previewManager.refresh();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 if (uiManager) // not available on the first call
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 uiManager.setUndoRedoButtonStates();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 }, panels);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 this.textOperation = function (f) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 undoManager.setCommandMode();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 f();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 that.refreshPreview();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 uiManager = new UIManager(idPostfix, panels, undoManager, previewManager, commandManager, options.helpButton, getString);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 uiManager.setUndoRedoButtonStates();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 var forceRefresh = that.refreshPreview = function () { previewManager.refresh(true); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 forceRefresh();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 // before: contains all the text in the input box BEFORE the selection.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 // after: contains all the text in the input box AFTER the selection.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 function Chunks() { }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 // startRegex: a regular expression to find the start tag
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 // endRegex: a regular expresssion to find the end tag
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 Chunks.prototype.findTags = function (startRegex, endRegex) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 var chunkObj = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 var regex;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 if (startRegex) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 regex = util.extendRegExp(startRegex, "", "$");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 this.before = this.before.replace(regex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 function (match) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 chunkObj.startTag = chunkObj.startTag + match;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 regex = util.extendRegExp(startRegex, "^", "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 this.selection = this.selection.replace(regex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 function (match) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 chunkObj.startTag = chunkObj.startTag + match;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 if (endRegex) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 regex = util.extendRegExp(endRegex, "", "$");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 this.selection = this.selection.replace(regex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 function (match) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 chunkObj.endTag = match + chunkObj.endTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 regex = util.extendRegExp(endRegex, "^", "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 this.after = this.after.replace(regex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 function (match) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 chunkObj.endTag = match + chunkObj.endTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 // If remove is false, the whitespace is transferred
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 // to the before/after regions.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 // If remove is true, the whitespace disappears.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 Chunks.prototype.trimWhitespace = function (remove) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 var beforeReplacer, afterReplacer, that = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 if (remove) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 beforeReplacer = afterReplacer = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 } else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 beforeReplacer = function (s) { that.before += s; return ""; }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 afterReplacer = function (s) { that.after = s + that.after; return ""; }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 this.selection = this.selection.replace(/^(\s*)/, beforeReplacer).replace(/(\s*)$/, afterReplacer);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 Chunks.prototype.skipLines = function (nLinesBefore, nLinesAfter, findExtraNewlines) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 if (nLinesBefore === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 nLinesBefore = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 if (nLinesAfter === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 nLinesAfter = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 nLinesBefore++;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 nLinesAfter++;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 var regexText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 var replacementText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235 // chrome bug ... documented at: http://meta.stackoverflow.com/questions/63307/blockquote-glitch-in-editor-in-chrome-6-and-7/65985#65985
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236 if (navigator.userAgent.match(/Chrome/)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 "X".match(/()./);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240 this.selection = this.selection.replace(/(^\n*)/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
241
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 this.startTag = this.startTag + re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
244 this.selection = this.selection.replace(/(\n*$)/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
245 this.endTag = this.endTag + re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
246 this.startTag = this.startTag.replace(/(^\n*)/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247 this.before = this.before + re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248 this.endTag = this.endTag.replace(/(\n*$)/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
249 this.after = this.after + re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
251 if (this.before) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
252
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
253 regexText = replacementText = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
254
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
255 while (nLinesBefore--) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
256 regexText += "\\n?";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257 replacementText += "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
260 if (findExtraNewlines) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
261 regexText = "\\n*";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
263 this.before = this.before.replace(new re(regexText + "$", ""), replacementText);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
264 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
265
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
266 if (this.after) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
267
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
268 regexText = replacementText = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
269
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
270 while (nLinesAfter--) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
271 regexText += "\\n?";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
272 replacementText += "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
273 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
274 if (findExtraNewlines) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
275 regexText = "\\n*";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
276 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
277
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
278 this.after = this.after.replace(new re(regexText, ""), replacementText);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
279 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
280 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
281
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
282 // end of Chunks
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
283
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
284 // A collection of the important regions on the page.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
285 // Cached so we don't have to keep traversing the DOM.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
286 // Also holds ieCachedRange and ieCachedScrollTop, where necessary; working around
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
287 // this issue:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
288 // Internet explorer has problems with CSS sprite buttons that use HTML
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
289 // lists. When you click on the background image "button", IE will
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
290 // select the non-existent link text and discard the selection in the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
291 // textarea. The solution to this is to cache the textarea selection
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
292 // on the button's mousedown event and set a flag. In the part of the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
293 // code where we need to grab the selection, we check for the flag
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
294 // and, if it's set, use the cached area instead of querying the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
295 // textarea.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
296 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
297 // This ONLY affects Internet Explorer (tested on versions 6, 7
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
298 // and 8) and ONLY on button clicks. Keyboard shortcuts work
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
299 // normally since the focus never leaves the textarea.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
300 function PanelCollection(postfix) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
301 this.buttonBar = doc.getElementById("wmd-button-bar" + postfix);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
302 this.preview = doc.getElementById("wmd-preview" + postfix);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
303 this.input = doc.getElementById("wmd-input" + postfix);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
304 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
305
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
306 // Returns true if the DOM element is visible, false if it's hidden.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
307 // Checks if display is anything other than none.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
308 util.isVisible = function (elem) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
309
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
310 if (window.getComputedStyle) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
311 // Most browsers
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
312 return window.getComputedStyle(elem, null).getPropertyValue("display") !== "none";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
313 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
314 else if (elem.currentStyle) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
315 // IE
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
316 return elem.currentStyle["display"] !== "none";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
317 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
318 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
319
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
320
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
321 // Adds a listener callback to a DOM element which is fired on a specified
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
322 // event.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
323 util.addEvent = function (elem, event, listener) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
324 if (elem.attachEvent) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
325 // IE only. The "on" is mandatory.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
326 elem.attachEvent("on" + event, listener);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
327 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
328 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
329 // Other browsers.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
330 elem.addEventListener(event, listener, false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
331 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
332 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
333
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
334
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
335 // Removes a listener callback from a DOM element which is fired on a specified
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
336 // event.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
337 util.removeEvent = function (elem, event, listener) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
338 if (elem.detachEvent) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
339 // IE only. The "on" is mandatory.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
340 elem.detachEvent("on" + event, listener);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
341 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
342 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
343 // Other browsers.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
344 elem.removeEventListener(event, listener, false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
345 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
346 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
347
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
348 // Converts \r\n and \r to \n.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
349 util.fixEolChars = function (text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
350 text = text.replace(/\r\n/g, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
351 text = text.replace(/\r/g, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
352 return text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
353 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
354
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
355 // Extends a regular expression. Returns a new RegExp
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
356 // using pre + regex + post as the expression.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
357 // Used in a few functions where we have a base
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
358 // expression and we want to pre- or append some
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
359 // conditions to it (e.g. adding "$" to the end).
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
360 // The flags are unchanged.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
361 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
362 // regex is a RegExp, pre and post are strings.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
363 util.extendRegExp = function (regex, pre, post) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
364
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
365 if (pre === null || pre === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
366 pre = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
367 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
368 if (post === null || post === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
369 post = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
370 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
371
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
372 var pattern = regex.toString();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
373 var flags;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
374
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
375 // Replace the flags with empty space and store them.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
376 pattern = pattern.replace(/\/([gim]*)$/, function (wholeMatch, flagsPart) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
377 flags = flagsPart;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
378 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
379 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
380
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
381 // Remove the slash delimiters on the regular expression.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
382 pattern = pattern.replace(/(^\/|\/$)/g, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
383 pattern = pre + pattern + post;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
384
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
385 return new re(pattern, flags);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
386 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
387
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
388 // UNFINISHED
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
389 // The assignment in the while loop makes jslint cranky.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
390 // I'll change it to a better loop later.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
391 position.getTop = function (elem, isInner) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
392 var result = elem.offsetTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
393 if (!isInner) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
394 while (elem = elem.offsetParent) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
395 result += elem.offsetTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
396 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
397 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
398 return result;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
399 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
400
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
401 position.getHeight = function (elem) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
402 return elem.offsetHeight || elem.scrollHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
403 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
404
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
405 position.getWidth = function (elem) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
406 return elem.offsetWidth || elem.scrollWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
407 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
408
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
409 position.getPageSize = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
410
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
411 var scrollWidth, scrollHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
412 var innerWidth, innerHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
413
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
414 // It's not very clear which blocks work with which browsers.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
415 if (self.innerHeight && self.scrollMaxY) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
416 scrollWidth = doc.body.scrollWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
417 scrollHeight = self.innerHeight + self.scrollMaxY;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
418 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
419 else if (doc.body.scrollHeight > doc.body.offsetHeight) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
420 scrollWidth = doc.body.scrollWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
421 scrollHeight = doc.body.scrollHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
422 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
423 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
424 scrollWidth = doc.body.offsetWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
425 scrollHeight = doc.body.offsetHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
426 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
427
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
428 if (self.innerHeight) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
429 // Non-IE browser
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
430 innerWidth = self.innerWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
431 innerHeight = self.innerHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
432 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
433 else if (doc.documentElement && doc.documentElement.clientHeight) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
434 // Some versions of IE (IE 6 w/ a DOCTYPE declaration)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
435 innerWidth = doc.documentElement.clientWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
436 innerHeight = doc.documentElement.clientHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
437 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
438 else if (doc.body) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
439 // Other versions of IE
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
440 innerWidth = doc.body.clientWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
441 innerHeight = doc.body.clientHeight;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
442 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
443
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
444 var maxWidth = Math.max(scrollWidth, innerWidth);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
445 var maxHeight = Math.max(scrollHeight, innerHeight);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
446 return [maxWidth, maxHeight, innerWidth, innerHeight];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
447 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
448
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
449 // Handles pushing and popping TextareaStates for undo/redo commands.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
450 // I should rename the stack variables to list.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
451 function UndoManager(callback, panels) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
452
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
453 var undoObj = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
454 var undoStack = []; // A stack of undo states
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
455 var stackPtr = 0; // The index of the current state
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
456 var mode = "none";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
457 var lastState; // The last state
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
458 var timer; // The setTimeout handle for cancelling the timer
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
459 var inputStateObj;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
460
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
461 // Set the mode for later logic steps.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
462 var setMode = function (newMode, noSave) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
463 if (mode != newMode) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
464 mode = newMode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
465 if (!noSave) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
466 saveState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
467 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
468 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
469
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
470 if (!uaSniffed.isIE || mode != "moving") {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
471 timer = setTimeout(refreshState, 1);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
472 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
473 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
474 inputStateObj = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
475 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
476 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
477
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
478 var refreshState = function (isInitialState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
479 inputStateObj = new TextareaState(panels, isInitialState);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
480 timer = undefined;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
481 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
482
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
483 this.setCommandMode = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
484 mode = "command";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
485 saveState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
486 timer = setTimeout(refreshState, 0);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
487 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
488
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
489 this.canUndo = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
490 return stackPtr > 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
491 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
492
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
493 this.canRedo = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
494 if (undoStack[stackPtr + 1]) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
495 return true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
496 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
497 return false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
498 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
499
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
500 // Removes the last state and restores it.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
501 this.undo = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
502
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
503 if (undoObj.canUndo()) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
504 if (lastState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
505 // What about setting state -1 to null or checking for undefined?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
506 lastState.restore();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
507 lastState = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
508 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
509 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
510 undoStack[stackPtr] = new TextareaState(panels);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
511 undoStack[--stackPtr].restore();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
512
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
513 if (callback) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
514 callback();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
515 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
516 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
517 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
518
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
519 mode = "none";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
520 panels.input.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
521 refreshState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
522 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
523
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
524 // Redo an action.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
525 this.redo = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
526
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
527 if (undoObj.canRedo()) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
528
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
529 undoStack[++stackPtr].restore();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
530
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
531 if (callback) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
532 callback();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
533 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
534 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
535
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
536 mode = "none";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
537 panels.input.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
538 refreshState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
539 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
540
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
541 // Push the input area state to the stack.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
542 var saveState = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
543 var currState = inputStateObj || new TextareaState(panels);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
544
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
545 if (!currState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
546 return false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
547 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
548 if (mode == "moving") {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
549 if (!lastState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
550 lastState = currState;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
551 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
552 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
553 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
554 if (lastState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
555 if (undoStack[stackPtr - 1].text != lastState.text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
556 undoStack[stackPtr++] = lastState;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
557 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
558 lastState = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
559 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
560 undoStack[stackPtr++] = currState;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
561 undoStack[stackPtr + 1] = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
562 if (callback) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
563 callback();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
564 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
565 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
566
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
567 var handleCtrlYZ = function (event) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
568
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
569 var handled = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
570
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
571 if (event.ctrlKey || event.metaKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
572
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
573 // IE and Opera do not support charCode.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
574 var keyCode = event.charCode || event.keyCode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
575 var keyCodeChar = String.fromCharCode(keyCode);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
576
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
577 switch (keyCodeChar.toLowerCase()) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
578
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
579 case "y":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
580 undoObj.redo();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
581 handled = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
582 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
583
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
584 case "z":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
585 if (!event.shiftKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
586 undoObj.undo();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
587 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
588 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
589 undoObj.redo();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
590 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
591 handled = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
592 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
593 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
594 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
595
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
596 if (handled) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
597 if (event.preventDefault) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
598 event.preventDefault();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
599 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
600 if (window.event) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
601 window.event.returnValue = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
602 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
603 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
604 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
605 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
606
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
607 // Set the mode depending on what is going on in the input area.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
608 var handleModeChange = function (event) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
609
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
610 if (!event.ctrlKey && !event.metaKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
611
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
612 var keyCode = event.keyCode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
613
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
614 if ((keyCode >= 33 && keyCode <= 40) || (keyCode >= 63232 && keyCode <= 63235)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
615 // 33 - 40: page up/dn and arrow keys
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
616 // 63232 - 63235: page up/dn and arrow keys on safari
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
617 setMode("moving");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
618 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
619 else if (keyCode == 8 || keyCode == 46 || keyCode == 127) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
620 // 8: backspace
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
621 // 46: delete
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
622 // 127: delete
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
623 setMode("deleting");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
624 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
625 else if (keyCode == 13) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
626 // 13: Enter
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
627 setMode("newlines");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
628 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
629 else if (keyCode == 27) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
630 // 27: escape
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
631 setMode("escape");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
632 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
633 else if ((keyCode < 16 || keyCode > 20) && keyCode != 91) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
634 // 16-20 are shift, etc.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
635 // 91: left window key
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
636 // I think this might be a little messed up since there are
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
637 // a lot of nonprinting keys above 20.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
638 setMode("typing");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
639 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
640 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
641 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
642
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
643 var setEventHandlers = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
644 util.addEvent(panels.input, "keypress", function (event) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
645 // keyCode 89: y
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
646 // keyCode 90: z
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
647 if ((event.ctrlKey || event.metaKey) && (event.keyCode == 89 || event.keyCode == 90)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
648 event.preventDefault();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
649 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
650 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
651
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
652 var handlePaste = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
653 if (uaSniffed.isIE || (inputStateObj && inputStateObj.text != panels.input.value)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
654 if (timer == undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
655 mode = "paste";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
656 saveState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
657 refreshState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
658 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
659 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
660 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
661
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
662 util.addEvent(panels.input, "keydown", handleCtrlYZ);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
663 util.addEvent(panels.input, "keydown", handleModeChange);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
664 util.addEvent(panels.input, "mousedown", function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
665 setMode("moving");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
666 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
667
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
668 panels.input.onpaste = handlePaste;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
669 panels.input.ondrop = handlePaste;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
670 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
671
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
672 var init = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
673 setEventHandlers();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
674 refreshState(true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
675 saveState();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
676 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
677
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
678 init();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
679 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
680
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
681 // end of UndoManager
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
682
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
683 // The input textarea state/contents.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
684 // This is used to implement undo/redo by the undo manager.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
685 function TextareaState(panels, isInitialState) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
686
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
687 // Aliases
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
688 var stateObj = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
689 var inputArea = panels.input;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
690 this.init = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
691 if (!util.isVisible(inputArea)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
692 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
693 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
694 if (!isInitialState && doc.activeElement && doc.activeElement !== inputArea) { // this happens when tabbing out of the input box
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
695 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
696 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
697
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
698 this.setInputAreaSelectionStartEnd();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
699 this.scrollTop = inputArea.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
700 if (!this.text && inputArea.selectionStart || inputArea.selectionStart === 0) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
701 this.text = inputArea.value;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
702 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
703
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
704 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
705
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
706 // Sets the selected text in the input box after we've performed an
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
707 // operation.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
708 this.setInputAreaSelection = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
709
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
710 if (!util.isVisible(inputArea)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
711 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
712 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
713
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
714 if (inputArea.selectionStart !== undefined && !uaSniffed.isOpera) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
715
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
716 inputArea.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
717 inputArea.selectionStart = stateObj.start;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
718 inputArea.selectionEnd = stateObj.end;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
719 inputArea.scrollTop = stateObj.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
720 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
721 else if (doc.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
722
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
723 if (doc.activeElement && doc.activeElement !== inputArea) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
724 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
725 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
726
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
727 inputArea.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
728 var range = inputArea.createTextRange();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
729 range.moveStart("character", -inputArea.value.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
730 range.moveEnd("character", -inputArea.value.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
731 range.moveEnd("character", stateObj.end);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
732 range.moveStart("character", stateObj.start);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
733 range.select();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
734 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
735 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
736
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
737 this.setInputAreaSelectionStartEnd = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
738
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
739 if (!panels.ieCachedRange && (inputArea.selectionStart || inputArea.selectionStart === 0)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
740
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
741 stateObj.start = inputArea.selectionStart;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
742 stateObj.end = inputArea.selectionEnd;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
743 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
744 else if (doc.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
745
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
746 stateObj.text = util.fixEolChars(inputArea.value);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
747
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
748 // IE loses the selection in the textarea when buttons are
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
749 // clicked. On IE we cache the selection. Here, if something is cached,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
750 // we take it.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
751 var range = panels.ieCachedRange || doc.selection.createRange();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
752
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
753 var fixedRange = util.fixEolChars(range.text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
754 var marker = "\x07";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
755 var markedRange = marker + fixedRange + marker;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
756 range.text = markedRange;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
757 var inputText = util.fixEolChars(inputArea.value);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
758
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
759 range.moveStart("character", -markedRange.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
760 range.text = fixedRange;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
761
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
762 stateObj.start = inputText.indexOf(marker);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
763 stateObj.end = inputText.lastIndexOf(marker) - marker.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
764
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
765 var len = stateObj.text.length - util.fixEolChars(inputArea.value).length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
766
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
767 if (len) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
768 range.moveStart("character", -fixedRange.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
769 while (len--) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
770 fixedRange += "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
771 stateObj.end += 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
772 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
773 range.text = fixedRange;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
774 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
775
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
776 if (panels.ieCachedRange)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
777 stateObj.scrollTop = panels.ieCachedScrollTop; // this is set alongside with ieCachedRange
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
778
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
779 panels.ieCachedRange = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
780
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
781 this.setInputAreaSelection();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
782 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
783 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
784
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
785 // Restore this state into the input area.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
786 this.restore = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
787
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
788 if (stateObj.text != undefined && stateObj.text != inputArea.value) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
789 inputArea.value = stateObj.text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
790 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
791 this.setInputAreaSelection();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
792 inputArea.scrollTop = stateObj.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
793 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
794
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
795 // Gets a collection of HTML chunks from the inptut textarea.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
796 this.getChunks = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
797
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
798 var chunk = new Chunks();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
799 chunk.before = util.fixEolChars(stateObj.text.substring(0, stateObj.start));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
800 chunk.startTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
801 chunk.selection = util.fixEolChars(stateObj.text.substring(stateObj.start, stateObj.end));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
802 chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
803 chunk.after = util.fixEolChars(stateObj.text.substring(stateObj.end));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
804 chunk.scrollTop = stateObj.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
805
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
806 return chunk;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
807 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
808
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
809 // Sets the TextareaState properties given a chunk of markdown.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
810 this.setChunks = function (chunk) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
811
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
812 chunk.before = chunk.before + chunk.startTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
813 chunk.after = chunk.endTag + chunk.after;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
814
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
815 this.start = chunk.before.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
816 this.end = chunk.before.length + chunk.selection.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
817 this.text = chunk.before + chunk.selection + chunk.after;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
818 this.scrollTop = chunk.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
819 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
820 this.init();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
821 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
822
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
823 function PreviewManager(converter, panels, previewRefreshCallback) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
824
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
825 var managerObj = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
826 var timeout;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
827 var elapsedTime;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
828 var oldInputText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
829 var maxDelay = 3000;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
830 var startType = "delayed"; // The other legal value is "manual"
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
831
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
832 // Adds event listeners to elements
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
833 var setupEvents = function (inputElem, listener) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
834
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
835 util.addEvent(inputElem, "input", listener);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
836 inputElem.onpaste = listener;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
837 inputElem.ondrop = listener;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
838
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
839 util.addEvent(inputElem, "keypress", listener);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
840 util.addEvent(inputElem, "keydown", listener);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
841 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
842
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
843 var getDocScrollTop = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
844
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
845 var result = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
846
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
847 if (window.innerHeight) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
848 result = window.pageYOffset;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
849 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
850 else
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
851 if (doc.documentElement && doc.documentElement.scrollTop) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
852 result = doc.documentElement.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
853 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
854 else
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
855 if (doc.body) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
856 result = doc.body.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
857 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
858
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
859 return result;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
860 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
861
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
862 var makePreviewHtml = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
863
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
864 // If there is no registered preview panel
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
865 // there is nothing to do.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
866 if (!panels.preview)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
867 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
868
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
869 var text = panels.input.value;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
870 if (text && text == oldInputText) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
871 return; // Input text hasn't changed.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
872 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
873 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
874 oldInputText = text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
875 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
876
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
877 var prevTime = new Date().getTime();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
878
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
879 text = converter.makeHtml(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
880
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
881 // Calculate the processing time of the HTML creation.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
882 // It's used as the delay time in the event listener.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
883 var currTime = new Date().getTime();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
884 elapsedTime = currTime - prevTime;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
885
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
886 pushPreviewHtml(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
887 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
888
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
889 // setTimeout is already used. Used as an event listener.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
890 var applyTimeout = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
891
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
892 if (timeout) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
893 clearTimeout(timeout);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
894 timeout = undefined;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
895 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
896
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
897 if (startType !== "manual") {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
898
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
899 var delay = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
900
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
901 if (startType === "delayed") {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
902 delay = elapsedTime;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
903 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
904
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
905 if (delay > maxDelay) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
906 delay = maxDelay;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
907 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
908 timeout = setTimeout(makePreviewHtml, delay);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
909 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
910 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
911
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
912 var getScaleFactor = function (panel) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
913 if (panel.scrollHeight <= panel.clientHeight) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
914 return 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
915 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
916 return panel.scrollTop / (panel.scrollHeight - panel.clientHeight);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
917 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
918
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
919 var setPanelScrollTops = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
920 if (panels.preview) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
921 panels.preview.scrollTop = (panels.preview.scrollHeight - panels.preview.clientHeight) * getScaleFactor(panels.preview);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
922 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
923 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
924
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
925 this.refresh = function (requiresRefresh) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
926
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
927 if (requiresRefresh) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
928 oldInputText = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
929 makePreviewHtml();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
930 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
931 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
932 applyTimeout();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
933 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
934 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
935
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
936 this.processingTime = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
937 return elapsedTime;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
938 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
939
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
940 var isFirstTimeFilled = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
941
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
942 // IE doesn't let you use innerHTML if the element is contained somewhere in a table
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
943 // (which is the case for inline editing) -- in that case, detach the element, set the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
944 // value, and reattach. Yes, that *is* ridiculous.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
945 var ieSafePreviewSet = function (text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
946 var preview = panels.preview;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
947 var parent = preview.parentNode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
948 var sibling = preview.nextSibling;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
949 parent.removeChild(preview);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
950 preview.innerHTML = text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
951 if (!sibling)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
952 parent.appendChild(preview);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
953 else
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
954 parent.insertBefore(preview, sibling);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
955 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
956
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
957 var nonSuckyBrowserPreviewSet = function (text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
958 panels.preview.innerHTML = text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
959 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
960
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
961 var previewSetter;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
962
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
963 var previewSet = function (text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
964 if (previewSetter)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
965 return previewSetter(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
966
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
967 try {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
968 nonSuckyBrowserPreviewSet(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
969 previewSetter = nonSuckyBrowserPreviewSet;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
970 } catch (e) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
971 previewSetter = ieSafePreviewSet;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
972 previewSetter(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
973 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
974 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
975
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
976 var pushPreviewHtml = function (text) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
977
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
978 var emptyTop = position.getTop(panels.input) - getDocScrollTop();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
979
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
980 if (panels.preview) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
981 previewSet(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
982 previewRefreshCallback();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
983 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
984
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
985 setPanelScrollTops();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
986
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
987 if (isFirstTimeFilled) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
988 isFirstTimeFilled = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
989 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
990 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
991
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
992 var fullTop = position.getTop(panels.input) - getDocScrollTop();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
993
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
994 if (uaSniffed.isIE) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
995 setTimeout(function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
996 window.scrollBy(0, fullTop - emptyTop);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
997 }, 0);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
998 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
999 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1000 window.scrollBy(0, fullTop - emptyTop);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1001 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1002 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1003
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1004 var init = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1005
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1006 setupEvents(panels.input, applyTimeout);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1007 makePreviewHtml();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1008
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1009 if (panels.preview) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1010 panels.preview.scrollTop = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1011 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1012 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1013
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1014 init();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1015 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1016
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1017 // Creates the background behind the hyperlink text entry box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1018 // And download dialog
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1019 // Most of this has been moved to CSS but the div creation and
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1020 // browser-specific hacks remain here.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1021 ui.createBackground = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1022
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1023 var background = doc.createElement("div"),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1024 style = background.style;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1025
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1026 background.className = "wmd-prompt-background";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1027
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1028 style.position = "absolute";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1029 style.top = "0";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1030
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1031 style.zIndex = "1000";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1032
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1033 if (uaSniffed.isIE) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1034 style.filter = "alpha(opacity=50)";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1035 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1036 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1037 style.opacity = "0.5";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1038 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1039
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1040 var pageSize = position.getPageSize();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1041 style.height = pageSize[1] + "px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1042
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1043 if (uaSniffed.isIE) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1044 style.left = doc.documentElement.scrollLeft;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1045 style.width = doc.documentElement.clientWidth;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1046 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1047 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1048 style.left = "0";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1049 style.width = "100%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1050 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1051
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1052 doc.body.appendChild(background);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1053 return background;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1054 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1055
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1056 // This simulates a modal dialog box and asks for the URL when you
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1057 // click the hyperlink or image buttons.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1058 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1059 // text: The html for the input box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1060 // defaultInputText: The default value that appears in the input box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1061 // callback: The function which is executed when the prompt is dismissed, either via OK or Cancel.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1062 // It receives a single argument; either the entered text (if OK was chosen) or null (if Cancel
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1063 // was chosen).
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1064 ui.prompt = function (text, defaultInputText, callback) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1065
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1066 // These variables need to be declared at this level since they are used
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1067 // in multiple functions.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1068 var dialog; // The dialog box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1069 var input; // The text box where you enter the hyperlink.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1070
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1071
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1072 if (defaultInputText === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1073 defaultInputText = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1074 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1075
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1076 // Used as a keydown event handler. Esc dismisses the prompt.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1077 // Key code 27 is ESC.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1078 var checkEscape = function (key) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1079 var code = (key.charCode || key.keyCode);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1080 if (code === 27) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1081 close(true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1082 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1083 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1084
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1085 // Dismisses the hyperlink input box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1086 // isCancel is true if we don't care about the input text.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1087 // isCancel is false if we are going to keep the text.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1088 var close = function (isCancel) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1089 util.removeEvent(doc.body, "keydown", checkEscape);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1090 var text = input.value;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1091
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1092 if (isCancel) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1093 text = null;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1094 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1095 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1096 // Fixes common pasting errors.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1097 text = text.replace(/^http:\/\/(https?|ftp):\/\//, '$1://');
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1098 if (!/^(?:https?|ftp):\/\//.test(text))
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1099 text = 'http://' + text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1100 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1101
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1102 dialog.parentNode.removeChild(dialog);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1103
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1104 callback(text);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1105 return false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1106 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1107
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1108
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1109
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1110 // Create the text input box form/window.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1111 var createDialog = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1112
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1113 // The main dialog box.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1114 dialog = doc.createElement("div");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1115 dialog.className = "wmd-prompt-dialog";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1116 dialog.style.padding = "10px;";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1117 dialog.style.position = "fixed";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1118 dialog.style.width = "400px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1119 dialog.style.zIndex = "1001";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1120
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1121 // The dialog text.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1122 var question = doc.createElement("div");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1123 question.innerHTML = text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1124 question.style.padding = "5px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1125 dialog.appendChild(question);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1126
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1127 // The web form container for the text box and buttons.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1128 var form = doc.createElement("form"),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1129 style = form.style;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1130 form.onsubmit = function () { return close(false); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1131 style.padding = "0";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1132 style.margin = "0";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1133 style.cssFloat = "left";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1134 style.width = "100%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1135 style.textAlign = "center";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1136 style.position = "relative";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1137 dialog.appendChild(form);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1138
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1139 // The input text box
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1140 input = doc.createElement("input");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1141 input.type = "text";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1142 input.value = defaultInputText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1143 style = input.style;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1144 style.display = "block";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1145 style.width = "80%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1146 style.marginLeft = style.marginRight = "auto";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1147 form.appendChild(input);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1148
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1149 // The ok button
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1150 var okButton = doc.createElement("input");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1151 okButton.type = "button";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1152 okButton.onclick = function () { return close(false); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1153 okButton.value = "OK";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1154 style = okButton.style;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1155 style.margin = "10px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1156 style.display = "inline";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1157 style.width = "7em";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1158
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1159
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1160 // The cancel button
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1161 var cancelButton = doc.createElement("input");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1162 cancelButton.type = "button";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1163 cancelButton.onclick = function () { return close(true); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1164 cancelButton.value = "Cancel";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1165 style = cancelButton.style;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1166 style.margin = "10px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1167 style.display = "inline";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1168 style.width = "7em";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1169
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1170 form.appendChild(okButton);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1171 form.appendChild(cancelButton);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1172
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1173 util.addEvent(doc.body, "keydown", checkEscape);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1174 dialog.style.top = "50%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1175 dialog.style.left = "50%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1176 dialog.style.display = "block";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1177 if (uaSniffed.isIE_5or6) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1178 dialog.style.position = "absolute";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1179 dialog.style.top = doc.documentElement.scrollTop + 200 + "px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1180 dialog.style.left = "50%";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1181 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1182 doc.body.appendChild(dialog);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1183
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1184 // This has to be done AFTER adding the dialog to the form if you
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1185 // want it to be centered.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1186 dialog.style.marginTop = -(position.getHeight(dialog) / 2) + "px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1187 dialog.style.marginLeft = -(position.getWidth(dialog) / 2) + "px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1188
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1189 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1190
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1191 // Why is this in a zero-length timeout?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1192 // Is it working around a browser bug?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1193 setTimeout(function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1194
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1195 createDialog();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1196
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1197 var defTextLen = defaultInputText.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1198 if (input.selectionStart !== undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1199 input.selectionStart = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1200 input.selectionEnd = defTextLen;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1201 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1202 else if (input.createTextRange) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1203 var range = input.createTextRange();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1204 range.collapse(false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1205 range.moveStart("character", -defTextLen);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1206 range.moveEnd("character", defTextLen);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1207 range.select();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1208 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1209
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1210 input.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1211 }, 0);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1212 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1213
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1214 function UIManager(postfix, panels, undoManager, previewManager, commandManager, helpOptions, getString) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1215
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1216 var inputBox = panels.input,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1217 buttons = {}; // buttons.undo, buttons.link, etc. The actual DOM elements.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1218
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1219 makeSpritedButtonRow();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1220
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1221 var keyEvent = "keydown";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1222 if (uaSniffed.isOpera) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1223 keyEvent = "keypress";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1224 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1225
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1226 util.addEvent(inputBox, keyEvent, function (key) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1227
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1228 // Check to see if we have a button key and, if so execute the callback.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1229 if ((key.ctrlKey || key.metaKey) && !key.altKey && !key.shiftKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1230
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1231 var keyCode = key.charCode || key.keyCode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1232 var keyCodeStr = String.fromCharCode(keyCode).toLowerCase();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1233
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1234 switch (keyCodeStr) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1235 case "b":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1236 doClick(buttons.bold);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1237 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1238 case "i":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1239 doClick(buttons.italic);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1240 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1241 case "l":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1242 doClick(buttons.link);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1243 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1244 case "q":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1245 doClick(buttons.quote);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1246 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1247 case "k":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1248 doClick(buttons.code);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1249 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1250 case "g":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1251 doClick(buttons.image);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1252 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1253 case "o":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1254 doClick(buttons.olist);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1255 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1256 case "u":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1257 doClick(buttons.ulist);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1258 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1259 case "h":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1260 doClick(buttons.heading);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1261 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1262 case "r":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1263 doClick(buttons.hr);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1264 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1265 case "y":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1266 doClick(buttons.redo);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1267 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1268 case "z":
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1269 if (key.shiftKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1270 doClick(buttons.redo);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1271 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1272 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1273 doClick(buttons.undo);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1274 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1275 break;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1276 default:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1277 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1278 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1279
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1280
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1281 if (key.preventDefault) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1282 key.preventDefault();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1283 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1284
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1285 if (window.event) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1286 window.event.returnValue = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1287 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1288 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1289 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1290
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1291 // Auto-indent on shift-enter
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1292 util.addEvent(inputBox, "keyup", function (key) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1293 if (key.shiftKey && !key.ctrlKey && !key.metaKey) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1294 var keyCode = key.charCode || key.keyCode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1295 // Character 13 is Enter
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1296 if (keyCode === 13) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1297 var fakeButton = {};
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1298 fakeButton.textOp = bindCommand("doAutoindent");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1299 doClick(fakeButton);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1300 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1301 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1302 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1303
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1304 // special handler because IE clears the context of the textbox on ESC
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1305 if (uaSniffed.isIE) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1306 util.addEvent(inputBox, "keydown", function (key) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1307 var code = key.keyCode;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1308 if (code === 27) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1309 return false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1310 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1311 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1312 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1313
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1314
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1315 // Perform the button's action.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1316 function doClick(button) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1317
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1318 inputBox.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1319
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1320 if (button.textOp) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1321
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1322 if (undoManager) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1323 undoManager.setCommandMode();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1324 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1325
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1326 var state = new TextareaState(panels);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1327
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1328 if (!state) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1329 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1330 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1331
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1332 var chunks = state.getChunks();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1333
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1334 // Some commands launch a "modal" prompt dialog. Javascript
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1335 // can't really make a modal dialog box and the WMD code
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1336 // will continue to execute while the dialog is displayed.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1337 // This prevents the dialog pattern I'm used to and means
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1338 // I can't do something like this:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1339 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1340 // var link = CreateLinkDialog();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1341 // makeMarkdownLink(link);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1342 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1343 // Instead of this straightforward method of handling a
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1344 // dialog I have to pass any code which would execute
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1345 // after the dialog is dismissed (e.g. link creation)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1346 // in a function parameter.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1347 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1348 // Yes this is awkward and I think it sucks, but there's
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1349 // no real workaround. Only the image and link code
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1350 // create dialogs and require the function pointers.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1351 var fixupInputArea = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1352
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1353 inputBox.focus();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1354
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1355 if (chunks) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1356 state.setChunks(chunks);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1357 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1358
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1359 state.restore();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1360 previewManager.refresh();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1361 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1362
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1363 var noCleanup = button.textOp(chunks, fixupInputArea);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1364
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1365 if (!noCleanup) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1366 fixupInputArea();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1367 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1368
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1369 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1370
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1371 if (button.execute) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1372 button.execute(undoManager);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1373 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1374 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1375
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1376 function setupButton(button, isEnabled) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1377
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1378 var normalYShift = "0px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1379 var disabledYShift = "-20px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1380 var highlightYShift = "-40px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1381 var image = button.getElementsByTagName("span")[0];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1382 if (isEnabled) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1383 image.style.backgroundPosition = button.XShift + " " + normalYShift;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1384 button.onmouseover = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1385 image.style.backgroundPosition = this.XShift + " " + highlightYShift;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1386 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1387
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1388 button.onmouseout = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1389 image.style.backgroundPosition = this.XShift + " " + normalYShift;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1390 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1391
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1392 // IE tries to select the background image "button" text (it's
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1393 // implemented in a list item) so we have to cache the selection
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1394 // on mousedown.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1395 if (uaSniffed.isIE) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1396 button.onmousedown = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1397 if (doc.activeElement && doc.activeElement !== panels.input) { // we're not even in the input box, so there's no selection
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1398 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1399 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1400 panels.ieCachedRange = document.selection.createRange();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1401 panels.ieCachedScrollTop = panels.input.scrollTop;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1402 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1403 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1404
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1405 if (!button.isHelp) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1406 button.onclick = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1407 if (this.onmouseout) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1408 this.onmouseout();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1409 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1410 doClick(this);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1411 return false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1412 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1413 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1414 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1415 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1416 image.style.backgroundPosition = button.XShift + " " + disabledYShift;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1417 button.onmouseover = button.onmouseout = button.onclick = function () { };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1418 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1419 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1420
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1421 function bindCommand(method) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1422 if (typeof method === "string")
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1423 method = commandManager[method];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1424 return function () { method.apply(commandManager, arguments); }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1425 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1426
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1427 function makeSpritedButtonRow() {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1428
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1429 var buttonBar = panels.buttonBar;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1430
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1431 var normalYShift = "0px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1432 var disabledYShift = "-20px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1433 var highlightYShift = "-40px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1434
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1435 var buttonRow = document.createElement("ul");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1436 buttonRow.id = "wmd-button-row" + postfix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1437 buttonRow.className = 'wmd-button-row';
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1438 buttonRow = buttonBar.appendChild(buttonRow);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1439 var xPosition = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1440 var makeButton = function (id, title, XShift, textOp) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1441 var button = document.createElement("li");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1442 button.className = "wmd-button";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1443 button.style.left = xPosition + "px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1444 xPosition += 25;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1445 var buttonImage = document.createElement("span");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1446 button.id = id + postfix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1447 button.appendChild(buttonImage);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1448 button.title = title;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1449 button.XShift = XShift;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1450 if (textOp)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1451 button.textOp = textOp;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1452 setupButton(button, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1453 buttonRow.appendChild(button);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1454 return button;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1455 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1456 var makeSpacer = function (num) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1457 var spacer = document.createElement("li");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1458 spacer.className = "wmd-spacer wmd-spacer" + num;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1459 spacer.id = "wmd-spacer" + num + postfix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1460 buttonRow.appendChild(spacer);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1461 xPosition += 25;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1462 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1463
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1464 buttons.bold = makeButton("wmd-bold-button", getString("bold"), "0px", bindCommand("doBold"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1465 buttons.italic = makeButton("wmd-italic-button", getString("italic"), "-20px", bindCommand("doItalic"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1466 makeSpacer(1);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1467 buttons.link = makeButton("wmd-link-button", getString("link"), "-40px", bindCommand(function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1468 return this.doLinkOrImage(chunk, postProcessing, false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1469 }));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1470 buttons.quote = makeButton("wmd-quote-button", getString("quote"), "-60px", bindCommand("doBlockquote"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1471 buttons.code = makeButton("wmd-code-button", getString("code"), "-80px", bindCommand("doCode"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1472 buttons.image = makeButton("wmd-image-button", getString("image"), "-100px", bindCommand(function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1473 return this.doLinkOrImage(chunk, postProcessing, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1474 }));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1475 makeSpacer(2);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1476 buttons.olist = makeButton("wmd-olist-button", getString("olist"), "-120px", bindCommand(function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1477 this.doList(chunk, postProcessing, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1478 }));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1479 buttons.ulist = makeButton("wmd-ulist-button", getString("ulist"), "-140px", bindCommand(function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1480 this.doList(chunk, postProcessing, false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1481 }));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1482 buttons.heading = makeButton("wmd-heading-button", getString("heading"), "-160px", bindCommand("doHeading"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1483 buttons.hr = makeButton("wmd-hr-button", getString("hr"), "-180px", bindCommand("doHorizontalRule"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1484 makeSpacer(3);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1485 buttons.undo = makeButton("wmd-undo-button", getString("undo"), "-200px", null);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1486 buttons.undo.execute = function (manager) { if (manager) manager.undo(); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1487
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1488 var redoTitle = /win/.test(nav.platform.toLowerCase()) ?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1489 getString("redo") :
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1490 getString("redomac"); // mac and other non-Windows platforms
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1491
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1492 buttons.redo = makeButton("wmd-redo-button", redoTitle, "-220px", null);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1493 buttons.redo.execute = function (manager) { if (manager) manager.redo(); };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1494
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1495 if (helpOptions) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1496 var helpButton = document.createElement("li");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1497 var helpButtonImage = document.createElement("span");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1498 helpButton.appendChild(helpButtonImage);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1499 helpButton.className = "wmd-button wmd-help-button";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1500 helpButton.id = "wmd-help-button" + postfix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1501 helpButton.XShift = "-240px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1502 helpButton.isHelp = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1503 helpButton.style.right = "0px";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1504 helpButton.title = getString("help");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1505 helpButton.onclick = helpOptions.handler;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1506
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1507 setupButton(helpButton, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1508 buttonRow.appendChild(helpButton);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1509 buttons.help = helpButton;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1510 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1511
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1512 setUndoRedoButtonStates();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1513 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1514
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1515 function setUndoRedoButtonStates() {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1516 if (undoManager) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1517 setupButton(buttons.undo, undoManager.canUndo());
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1518 setupButton(buttons.redo, undoManager.canRedo());
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1519 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1520 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1521
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1522 this.setUndoRedoButtonStates = setUndoRedoButtonStates;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1523
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1524 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1525
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1526 function CommandManager(pluginHooks, getString) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1527 this.hooks = pluginHooks;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1528 this.getString = getString;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1529 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1530
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1531 var commandProto = CommandManager.prototype;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1532
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1533 // The markdown symbols - 4 spaces = code, > = blockquote, etc.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1534 commandProto.prefixes = "(?:\\s{4,}|\\s*>|\\s*-\\s+|\\s*\\d+\\.|=|\\+|-|_|\\*|#|\\s*\\[[^\n]]+\\]:)";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1535
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1536 // Remove markdown symbols from the chunk selection.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1537 commandProto.unwrap = function (chunk) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1538 var txt = new re("([^\\n])\\n(?!(\\n|" + this.prefixes + "))", "g");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1539 chunk.selection = chunk.selection.replace(txt, "$1 $2");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1540 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1541
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1542 commandProto.wrap = function (chunk, len) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1543 this.unwrap(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1544 var regex = new re("(.{1," + len + "})( +|$\\n?)", "gm"),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1545 that = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1546
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1547 chunk.selection = chunk.selection.replace(regex, function (line, marked) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1548 if (new re("^" + that.prefixes, "").test(line)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1549 return line;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1550 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1551 return marked + "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1552 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1553
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1554 chunk.selection = chunk.selection.replace(/\s+$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1555 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1556
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1557 commandProto.doBold = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1558 return this.doBorI(chunk, postProcessing, 2, this.getString("boldexample"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1559 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1560
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1561 commandProto.doItalic = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1562 return this.doBorI(chunk, postProcessing, 1, this.getString("italicexample"));
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1563 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1564
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1565 // chunk: The selected region that will be enclosed with */**
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1566 // nStars: 1 for italics, 2 for bold
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1567 // insertText: If you just click the button without highlighting text, this gets inserted
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1568 commandProto.doBorI = function (chunk, postProcessing, nStars, insertText) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1569
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1570 // Get rid of whitespace and fixup newlines.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1571 chunk.trimWhitespace();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1572 chunk.selection = chunk.selection.replace(/\n{2,}/g, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1573
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1574 // Look for stars before and after. Is the chunk already marked up?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1575 // note that these regex matches cannot fail
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1576 var starsBefore = /(\**$)/.exec(chunk.before)[0];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1577 var starsAfter = /(^\**)/.exec(chunk.after)[0];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1578
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1579 var prevStars = Math.min(starsBefore.length, starsAfter.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1580
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1581 // Remove stars if we have to since the button acts as a toggle.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1582 if ((prevStars >= nStars) && (prevStars != 2 || nStars != 1)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1583 chunk.before = chunk.before.replace(re("[*]{" + nStars + "}$", ""), "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1584 chunk.after = chunk.after.replace(re("^[*]{" + nStars + "}", ""), "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1585 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1586 else if (!chunk.selection && starsAfter) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1587 // It's not really clear why this code is necessary. It just moves
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1588 // some arbitrary stuff around.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1589 chunk.after = chunk.after.replace(/^([*_]*)/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1590 chunk.before = chunk.before.replace(/(\s?)$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1591 var whitespace = re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1592 chunk.before = chunk.before + starsAfter + whitespace;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1593 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1594 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1595
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1596 // In most cases, if you don't have any selected text and click the button
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1597 // you'll get a selected, marked up region with the default text inserted.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1598 if (!chunk.selection && !starsAfter) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1599 chunk.selection = insertText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1600 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1601
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1602 // Add the true markup.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1603 var markup = nStars <= 1 ? "*" : "**"; // shouldn't the test be = ?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1604 chunk.before = chunk.before + markup;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1605 chunk.after = markup + chunk.after;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1606 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1607
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1608 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1609 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1610
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1611 commandProto.stripLinkDefs = function (text, defsToAdd) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1612
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1613 text = text.replace(/^[ ]{0,3}\[(\d+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|$)/gm,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1614 function (totalMatch, id, link, newlines, title) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1615 defsToAdd[id] = totalMatch.replace(/\s*$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1616 if (newlines) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1617 // Strip the title and return that separately.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1618 defsToAdd[id] = totalMatch.replace(/["(](.+?)[")]$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1619 return newlines + title;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1620 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1621 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1622 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1623
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1624 return text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1625 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1626
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1627 commandProto.addLinkDef = function (chunk, linkDef) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1628
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1629 var refNumber = 0; // The current reference number
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1630 var defsToAdd = {}; //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1631 // Start with a clean slate by removing all previous link definitions.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1632 chunk.before = this.stripLinkDefs(chunk.before, defsToAdd);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1633 chunk.selection = this.stripLinkDefs(chunk.selection, defsToAdd);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1634 chunk.after = this.stripLinkDefs(chunk.after, defsToAdd);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1635
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1636 var defs = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1637 var regex = /(\[)((?:\[[^\]]*\]|[^\[\]])*)(\][ ]?(?:\n[ ]*)?\[)(\d+)(\])/g;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1638
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1639 var addDefNumber = function (def) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1640 refNumber++;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1641 def = def.replace(/^[ ]{0,3}\[(\d+)\]:/, " [" + refNumber + "]:");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1642 defs += "\n" + def;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1643 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1644
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1645 // note that
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1646 // a) the recursive call to getLink cannot go infinite, because by definition
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1647 // of regex, inner is always a proper substring of wholeMatch, and
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1648 // b) more than one level of nesting is neither supported by the regex
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1649 // nor making a lot of sense (the only use case for nesting is a linked image)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1650 var getLink = function (wholeMatch, before, inner, afterInner, id, end) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1651 inner = inner.replace(regex, getLink);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1652 if (defsToAdd[id]) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1653 addDefNumber(defsToAdd[id]);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1654 return before + inner + afterInner + refNumber + end;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1655 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1656 return wholeMatch;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1657 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1658
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1659 chunk.before = chunk.before.replace(regex, getLink);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1660
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1661 if (linkDef) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1662 addDefNumber(linkDef);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1663 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1664 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1665 chunk.selection = chunk.selection.replace(regex, getLink);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1666 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1667
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1668 var refOut = refNumber;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1669
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1670 chunk.after = chunk.after.replace(regex, getLink);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1671
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1672 if (chunk.after) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1673 chunk.after = chunk.after.replace(/\n*$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1674 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1675 if (!chunk.after) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1676 chunk.selection = chunk.selection.replace(/\n*$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1677 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1678
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1679 chunk.after += "\n\n" + defs;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1680
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1681 return refOut;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1682 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1683
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1684 // takes the line as entered into the add link/as image dialog and makes
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1685 // sure the URL and the optinal title are "nice".
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1686 function properlyEncoded(linkdef) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1687 return linkdef.replace(/^\s*(.*?)(?:\s+"(.+)")?\s*$/, function (wholematch, link, title) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1688 link = link.replace(/\?.*$/, function (querypart) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1689 return querypart.replace(/\+/g, " "); // in the query string, a plus and a space are identical
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1690 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1691 link = decodeURIComponent(link); // unencode first, to prevent double encoding
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1692 link = encodeURI(link).replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29');
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1693 link = link.replace(/\?.*$/, function (querypart) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1694 return querypart.replace(/\+/g, "%2b"); // since we replaced plus with spaces in the query part, all pluses that now appear where originally encoded
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1695 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1696 if (title) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1697 title = title.trim ? title.trim() : title.replace(/^\s*/, "").replace(/\s*$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1698 title = title.replace(/"/g, "quot;").replace(/\(/g, "&#40;").replace(/\)/g, "&#41;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1699 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1700 return title ? link + ' "' + title + '"' : link;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1701 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1702 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1703
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1704 commandProto.doLinkOrImage = function (chunk, postProcessing, isImage) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1705
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1706 chunk.trimWhitespace();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1707 chunk.findTags(/\s*!?\[/, /\][ ]?(?:\n[ ]*)?(\[.*?\])?/);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1708 var background;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1709
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1710 if (chunk.endTag.length > 1 && chunk.startTag.length > 0) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1711
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1712 chunk.startTag = chunk.startTag.replace(/!?\[/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1713 chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1714 this.addLinkDef(chunk, null);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1715
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1716 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1717 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1718
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1719 // We're moving start and end tag back into the selection, since (as we're in the else block) we're not
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1720 // *removing* a link, but *adding* one, so whatever findTags() found is now back to being part of the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1721 // link text. linkEnteredCallback takes care of escaping any brackets.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1722 chunk.selection = chunk.startTag + chunk.selection + chunk.endTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1723 chunk.startTag = chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1724
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1725 if (/\n\n/.test(chunk.selection)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1726 this.addLinkDef(chunk, null);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1727 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1728 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1729 var that = this;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1730 // The function to be executed when you enter a link and press OK or Cancel.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1731 // Marks up the link and adds the ref.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1732 var linkEnteredCallback = function (link) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1733
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1734 background.parentNode.removeChild(background);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1735
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1736 if (link !== null) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1737 // ( $1
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1738 // [^\\] anything that's not a backslash
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1739 // (?:\\\\)* an even number (this includes zero) of backslashes
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1740 // )
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1741 // (?= followed by
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1742 // [[\]] an opening or closing bracket
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1743 // )
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1744 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1745 // In other words, a non-escaped bracket. These have to be escaped now to make sure they
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1746 // don't count as the end of the link or similar.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1747 // Note that the actual bracket has to be a lookahead, because (in case of to subsequent brackets),
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1748 // the bracket in one match may be the "not a backslash" character in the next match, so it
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1749 // should not be consumed by the first match.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1750 // The "prepend a space and finally remove it" steps makes sure there is a "not a backslash" at the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1751 // start of the string, so this also works if the selection begins with a bracket. We cannot solve
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1752 // this by anchoring with ^, because in the case that the selection starts with two brackets, this
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1753 // would mean a zero-width match at the start. Since zero-width matches advance the string position,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1754 // the first bracket could then not act as the "not a backslash" for the second.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1755 chunk.selection = (" " + chunk.selection).replace(/([^\\](?:\\\\)*)(?=[[\]])/g, "$1\\").substr(1);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1756
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1757 var linkDef = " [999]: " + properlyEncoded(link);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1758
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1759 var num = that.addLinkDef(chunk, linkDef);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1760 chunk.startTag = isImage ? "![" : "[";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1761 chunk.endTag = "][" + num + "]";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1762
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1763 if (!chunk.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1764 if (isImage) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1765 chunk.selection = that.getString("imagedescription");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1766 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1767 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1768 chunk.selection = that.getString("linkdescription");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1769 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1770 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1771 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1772 postProcessing();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1773 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1774
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1775 background = ui.createBackground();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1776
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1777 if (isImage) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1778 if (!this.hooks.insertImageDialog(linkEnteredCallback))
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1779 ui.prompt(this.getString("imagedialog"), imageDefaultText, linkEnteredCallback);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1780 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1781 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1782 ui.prompt(this.getString("linkdialog"), linkDefaultText, linkEnteredCallback);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1783 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1784 return true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1785 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1786 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1787
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1788 // When making a list, hitting shift-enter will put your cursor on the next line
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1789 // at the current indent level.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1790 commandProto.doAutoindent = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1791
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1792 var commandMgr = this,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1793 fakeSelection = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1794
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1795 chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]*\n$/, "\n\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1796 chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}>[ \t]*\n$/, "\n\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1797 chunk.before = chunk.before.replace(/(\n|^)[ \t]+\n$/, "\n\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1798
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1799 // There's no selection, end the cursor wasn't at the end of the line:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1800 // The user wants to split the current list item / code line / blockquote line
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1801 // (for the latter it doesn't really matter) in two. Temporarily select the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1802 // (rest of the) line to achieve this.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1803 if (!chunk.selection && !/^[ \t]*(?:\n|$)/.test(chunk.after)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1804 chunk.after = chunk.after.replace(/^[^\n]*/, function (wholeMatch) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1805 chunk.selection = wholeMatch;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1806 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1807 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1808 fakeSelection = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1809 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1810
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1811 if (/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(chunk.before)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1812 if (commandMgr.doList) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1813 commandMgr.doList(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1814 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1815 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1816 if (/(\n|^)[ ]{0,3}>[ \t]+.*\n$/.test(chunk.before)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1817 if (commandMgr.doBlockquote) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1818 commandMgr.doBlockquote(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1819 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1820 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1821 if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1822 if (commandMgr.doCode) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1823 commandMgr.doCode(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1824 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1825 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1826
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1827 if (fakeSelection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1828 chunk.after = chunk.selection + chunk.after;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1829 chunk.selection = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1830 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1831 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1832
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1833 commandProto.doBlockquote = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1834
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1835 chunk.selection = chunk.selection.replace(/^(\n*)([^\r]+?)(\n*)$/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1836 function (totalMatch, newlinesBefore, text, newlinesAfter) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1837 chunk.before += newlinesBefore;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1838 chunk.after = newlinesAfter + chunk.after;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1839 return text;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1840 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1841
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1842 chunk.before = chunk.before.replace(/(>[ \t]*)$/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1843 function (totalMatch, blankLine) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1844 chunk.selection = blankLine + chunk.selection;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1845 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1846 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1847
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1848 chunk.selection = chunk.selection.replace(/^(\s|>)+$/, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1849 chunk.selection = chunk.selection || this.getString("quoteexample");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1850
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1851 // The original code uses a regular expression to find out how much of the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1852 // text *directly before* the selection already was a blockquote:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1853
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1854 /*
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1855 if (chunk.before) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1856 chunk.before = chunk.before.replace(/\n?$/, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1857 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1858 chunk.before = chunk.before.replace(/(((\n|^)(\n[ \t]*)*>(.+\n)*.*)+(\n[ \t]*)*$)/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1859 function (totalMatch) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1860 chunk.startTag = totalMatch;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1861 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1862 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1863 */
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1864
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1865 // This comes down to:
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1866 // Go backwards as many lines a possible, such that each line
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1867 // a) starts with ">", or
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1868 // b) is almost empty, except for whitespace, or
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1869 // c) is preceeded by an unbroken chain of non-empty lines
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1870 // leading up to a line that starts with ">" and at least one more character
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1871 // and in addition
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1872 // d) at least one line fulfills a)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1873 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1874 // Since this is essentially a backwards-moving regex, it's susceptible to
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1875 // catstrophic backtracking and can cause the browser to hang;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1876 // see e.g. http://meta.stackoverflow.com/questions/9807.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1877 //
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1878 // Hence we replaced this by a simple state machine that just goes through the
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1879 // lines and checks for a), b), and c).
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1880
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1881 var match = "",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1882 leftOver = "",
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1883 line;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1884 if (chunk.before) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1885 var lines = chunk.before.replace(/\n$/, "").split("\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1886 var inChain = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1887 for (var i = 0; i < lines.length; i++) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1888 var good = false;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1889 line = lines[i];
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1890 inChain = inChain && line.length > 0; // c) any non-empty line continues the chain
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1891 if (/^>/.test(line)) { // a)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1892 good = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1893 if (!inChain && line.length > 1) // c) any line that starts with ">" and has at least one more character starts the chain
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1894 inChain = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1895 } else if (/^[ \t]*$/.test(line)) { // b)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1896 good = true;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1897 } else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1898 good = inChain; // c) the line is not empty and does not start with ">", so it matches if and only if we're in the chain
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1899 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1900 if (good) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1901 match += line + "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1902 } else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1903 leftOver += match + line;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1904 match = "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1905 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1906 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1907 if (!/(^|\n)>/.test(match)) { // d)
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1908 leftOver += match;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1909 match = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1910 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1911 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1912
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1913 chunk.startTag = match;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1914 chunk.before = leftOver;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1915
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1916 // end of change
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1917
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1918 if (chunk.after) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1919 chunk.after = chunk.after.replace(/^\n?/, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1920 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1921
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1922 chunk.after = chunk.after.replace(/^(((\n|^)(\n[ \t]*)*>(.+\n)*.*)+(\n[ \t]*)*)/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1923 function (totalMatch) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1924 chunk.endTag = totalMatch;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1925 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1926 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1927 );
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1928
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1929 var replaceBlanksInTags = function (useBracket) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1930
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1931 var replacement = useBracket ? "> " : "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1932
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1933 if (chunk.startTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1934 chunk.startTag = chunk.startTag.replace(/\n((>|\s)*)\n$/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1935 function (totalMatch, markdown) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1936 return "\n" + markdown.replace(/^[ ]{0,3}>?[ \t]*$/gm, replacement) + "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1937 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1938 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1939 if (chunk.endTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1940 chunk.endTag = chunk.endTag.replace(/^\n((>|\s)*)\n/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1941 function (totalMatch, markdown) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1942 return "\n" + markdown.replace(/^[ ]{0,3}>?[ \t]*$/gm, replacement) + "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1943 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1944 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1945 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1946
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1947 if (/^(?![ ]{0,3}>)/m.test(chunk.selection)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1948 this.wrap(chunk, SETTINGS.lineLength - 2);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1949 chunk.selection = chunk.selection.replace(/^/gm, "> ");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1950 replaceBlanksInTags(true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1951 chunk.skipLines();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1952 } else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1953 chunk.selection = chunk.selection.replace(/^[ ]{0,3}> ?/gm, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1954 this.unwrap(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1955 replaceBlanksInTags(false);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1956
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1957 if (!/^(\n|^)[ ]{0,3}>/.test(chunk.selection) && chunk.startTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1958 chunk.startTag = chunk.startTag.replace(/\n{0,2}$/, "\n\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1959 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1960
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1961 if (!/(\n|^)[ ]{0,3}>.*$/.test(chunk.selection) && chunk.endTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1962 chunk.endTag = chunk.endTag.replace(/^\n{0,2}/, "\n\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1963 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1964 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1965
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1966 chunk.selection = this.hooks.postBlockquoteCreation(chunk.selection);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1967
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1968 if (!/\n/.test(chunk.selection)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1969 chunk.selection = chunk.selection.replace(/^(> *)/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1970 function (wholeMatch, blanks) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1971 chunk.startTag += blanks;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1972 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1973 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1974 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1975 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1976
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1977 commandProto.doCode = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1978
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1979 var hasTextBefore = /\S[ ]*$/.test(chunk.before);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1980 var hasTextAfter = /^[ ]*\S/.test(chunk.after);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1981
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1982 // Use 'four space' markdown if the selection is on its own
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1983 // line or is multiline.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1984 if ((!hasTextAfter && !hasTextBefore) || /\n/.test(chunk.selection)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1985
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1986 chunk.before = chunk.before.replace(/[ ]{4}$/,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1987 function (totalMatch) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1988 chunk.selection = totalMatch + chunk.selection;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1989 return "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1990 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1991
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1992 var nLinesBack = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1993 var nLinesForward = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1994
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1995 if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1996 nLinesBack = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1997 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1998 if (/^\n(\t|[ ]{4,})/.test(chunk.after)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1999 nLinesForward = 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2000 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2001
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2002 chunk.skipLines(nLinesBack, nLinesForward);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2003
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2004 if (!chunk.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2005 chunk.startTag = " ";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2006 chunk.selection = this.getString("codeexample");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2007 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2008 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2009 if (/^[ ]{0,3}\S/m.test(chunk.selection)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2010 if (/\n/.test(chunk.selection))
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2011 chunk.selection = chunk.selection.replace(/^/gm, " ");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2012 else // if it's not multiline, do not select the four added spaces; this is more consistent with the doList behavior
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2013 chunk.before += " ";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2014 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2015 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2016 chunk.selection = chunk.selection.replace(/^[ ]{4}/gm, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2017 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2018 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2019 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2020 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2021 // Use backticks (`) to delimit the code block.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2022
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2023 chunk.trimWhitespace();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2024 chunk.findTags(/`/, /`/);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2025
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2026 if (!chunk.startTag && !chunk.endTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2027 chunk.startTag = chunk.endTag = "`";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2028 if (!chunk.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2029 chunk.selection = this.getString("codeexample");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2030 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2031 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2032 else if (chunk.endTag && !chunk.startTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2033 chunk.before += chunk.endTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2034 chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2035 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2036 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2037 chunk.startTag = chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2038 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2039 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2040 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2041
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2042 commandProto.doList = function (chunk, postProcessing, isNumberedList) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2043
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2044 // These are identical except at the very beginning and end.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2045 // Should probably use the regex extension function to make this clearer.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2046 var previousItemsRegex = /(\n|^)(([ ]{0,3}([*+-]|\d+[.])[ \t]+.*)(\n.+|\n{2,}([*+-].*|\d+[.])[ \t]+.*|\n{2,}[ \t]+\S.*)*)\n*$/;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2047 var nextItemsRegex = /^\n*(([ ]{0,3}([*+-]|\d+[.])[ \t]+.*)(\n.+|\n{2,}([*+-].*|\d+[.])[ \t]+.*|\n{2,}[ \t]+\S.*)*)\n*/;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2048
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2049 // The default bullet is a dash but others are possible.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2050 // This has nothing to do with the particular HTML bullet,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2051 // it's just a markdown bullet.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2052 var bullet = "-";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2053
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2054 // The number in a numbered list.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2055 var num = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2056
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2057 // Get the item prefix - e.g. " 1. " for a numbered list, " - " for a bulleted list.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2058 var getItemPrefix = function () {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2059 var prefix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2060 if (isNumberedList) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2061 prefix = " " + num + ". ";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2062 num++;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2063 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2064 else {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2065 prefix = " " + bullet + " ";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2066 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2067 return prefix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2068 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2069
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2070 // Fixes the prefixes of the other list items.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2071 var getPrefixedItem = function (itemText) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2072
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2073 // The numbering flag is unset when called by autoindent.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2074 if (isNumberedList === undefined) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2075 isNumberedList = /^\s*\d/.test(itemText);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2076 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2077
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2078 // Renumber/bullet the list element.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2079 itemText = itemText.replace(/^[ ]{0,3}([*+-]|\d+[.])\s/gm,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2080 function (_) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2081 return getItemPrefix();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2082 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2083
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2084 return itemText;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2085 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2086
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2087 chunk.findTags(/(\n|^)*[ ]{0,3}([*+-]|\d+[.])\s+/, null);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2088
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2089 if (chunk.before && !/\n$/.test(chunk.before) && !/^\n/.test(chunk.startTag)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2090 chunk.before += chunk.startTag;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2091 chunk.startTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2092 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2093
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2094 if (chunk.startTag) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2095
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2096 var hasDigits = /\d+[.]/.test(chunk.startTag);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2097 chunk.startTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2098 chunk.selection = chunk.selection.replace(/\n[ ]{4}/g, "\n");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2099 this.unwrap(chunk);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2100 chunk.skipLines();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2101
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2102 if (hasDigits) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2103 // Have to renumber the bullet points if this is a numbered list.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2104 chunk.after = chunk.after.replace(nextItemsRegex, getPrefixedItem);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2105 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2106 if (isNumberedList == hasDigits) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2107 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2108 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2109 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2110
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2111 var nLinesUp = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2112
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2113 chunk.before = chunk.before.replace(previousItemsRegex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2114 function (itemText) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2115 if (/^\s*([*+-])/.test(itemText)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2116 bullet = re.$1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2117 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2118 nLinesUp = /[^\n]\n\n[^\n]/.test(itemText) ? 1 : 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2119 return getPrefixedItem(itemText);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2120 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2121
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2122 if (!chunk.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2123 chunk.selection = this.getString("litem");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2124 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2125
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2126 var prefix = getItemPrefix();
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2127
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2128 var nLinesDown = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2129
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2130 chunk.after = chunk.after.replace(nextItemsRegex,
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2131 function (itemText) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2132 nLinesDown = /[^\n]\n\n[^\n]/.test(itemText) ? 1 : 0;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2133 return getPrefixedItem(itemText);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2134 });
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2135
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2136 chunk.trimWhitespace(true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2137 chunk.skipLines(nLinesUp, nLinesDown, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2138 chunk.startTag = prefix;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2139 var spaces = prefix.replace(/./g, " ");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2140 this.wrap(chunk, SETTINGS.lineLength - spaces.length);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2141 chunk.selection = chunk.selection.replace(/\n/g, "\n" + spaces);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2142
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2143 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2144
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2145 commandProto.doHeading = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2146
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2147 // Remove leading/trailing whitespace and reduce internal spaces to single spaces.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2148 chunk.selection = chunk.selection.replace(/\s+/g, " ");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2149 chunk.selection = chunk.selection.replace(/(^\s+|\s+$)/g, "");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2150
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2151 // If we clicked the button with no selected text, we just
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2152 // make a level 2 hash header around some default text.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2153 if (!chunk.selection) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2154 chunk.startTag = "## ";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2155 chunk.selection = this.getString("headingexample");
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2156 chunk.endTag = " ##";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2157 return;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2158 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2159
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2160 var headerLevel = 0; // The existing header level of the selected text.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2161
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2162 // Remove any existing hash heading markdown and save the header level.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2163 chunk.findTags(/#+[ ]*/, /[ ]*#+/);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2164 if (/#+/.test(chunk.startTag)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2165 headerLevel = re.lastMatch.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2166 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2167 chunk.startTag = chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2168
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2169 // Try to get the current header level by looking for - and = in the line
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2170 // below the selection.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2171 chunk.findTags(null, /\s?(-+|=+)/);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2172 if (/=+/.test(chunk.endTag)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2173 headerLevel = 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2174 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2175 if (/-+/.test(chunk.endTag)) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2176 headerLevel = 2;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2177 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2178
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2179 // Skip to the next line so we can create the header markdown.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2180 chunk.startTag = chunk.endTag = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2181 chunk.skipLines(1, 1);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2182
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2183 // We make a level 2 header if there is no current header.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2184 // If there is a header level, we substract one from the header level.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2185 // If it's already a level 1 header, it's removed.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2186 var headerLevelToCreate = headerLevel == 0 ? 2 : headerLevel - 1;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2187
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2188 if (headerLevelToCreate > 0) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2189
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2190 // The button only creates level 1 and 2 underline headers.
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2191 // Why not have it iterate over hash header levels? Wouldn't that be easier and cleaner?
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2192 var headerChar = headerLevelToCreate >= 2 ? "-" : "=";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2193 var len = chunk.selection.length;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2194 if (len > SETTINGS.lineLength) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2195 len = SETTINGS.lineLength;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2196 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2197 chunk.endTag = "\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2198 while (len--) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2199 chunk.endTag += headerChar;
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2200 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2201 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2202 };
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2203
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2204 commandProto.doHorizontalRule = function (chunk, postProcessing) {
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2205 chunk.startTag = "----------\n";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2206 chunk.selection = "";
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2207 chunk.skipLines(2, 1, true);
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2208 }
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2209
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2210
59cad6ce1a1c Added support for history and diffing.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2211 })();