From 68f8aea79ca01667abd233c1601fe7f298133f77 Mon Sep 17 00:00:00 2001 From: benweet Date: Mon, 27 May 2013 23:13:41 +0100 Subject: [PATCH] New extension pattern --- js/core.js | 42 +++++++++++++------------ js/extensions/button-sync.js | 4 ++- js/extensions/document-selector.js | 2 +- js/extensions/manage-publication.js | 2 +- js/extensions/manage-synchronization.js | 2 +- js/extensions/toc.js | 2 +- js/file-manager.js | 2 +- js/lib/Markdown.Converter.js | 23 ++++++++++---- js/lib/Markdown.Editor.js | 18 +++++------ js/main.js | 10 +++--- js/settings.js | 3 +- js/synchronizer.js | 2 +- 12 files changed, 64 insertions(+), 48 deletions(-) diff --git a/js/core.js b/js/core.js index d69e7f48..141c807f 100644 --- a/js/core.js +++ b/js/core.js @@ -230,32 +230,34 @@ define([ return true; }); - var firstChange = true; - var previewWrapper = function(makePreview) { - return function() { - if(firstChange !== true) { - onTextChange(); - } - makePreview(); - }; - }; + var documentContent = undefined; + function checkDocumentChanges() { + var newDocumentContent = $("#wmd-input").val(); + if(documentContent !== undefined && documentContent != newDocumentContent) { + onTextChange(); + } + documentContent = newDocumentContent; + } + var previewWrapper = undefined; if(settings.lazyRendering === true) { - var lastRefresh = 0; previewWrapper = function(makePreview) { - //var debouncedMakePreview = _.debounce(makePreview, 500); + var debouncedMakePreview = _.debounce(makePreview, 500); return function() { - if(firstChange === true) { + if(documentContent === undefined) { makePreview(); } else { - onTextChange(); - var currentDate = new Date().getTime(); - if(currentDate - lastRefresh > 500) { - makePreview(); - lastRefresh = currentDate; - } - //debouncedMakePreview(); + debouncedMakePreview(); } + checkDocumentChanges(); + }; + }; + } + else { + previewWrapper = function(makePreview) { + return function() { + checkDocumentChanges(); + makePreview(); }; }; } @@ -313,7 +315,7 @@ define([ } } - core.onReady(extensionManager.onReady); + core.onReady(extensionMgr.onReady); core.onReady(function() { // Load theme list diff --git a/js/extensions/button-sync.js b/js/extensions/button-sync.js index 0933c334..c557e803 100644 --- a/js/extensions/button-sync.js +++ b/js/extensions/button-sync.js @@ -14,7 +14,7 @@ define([ var uploadPending = false; var isOffline = false; // Enable/disable the button - function updateButtonState() { + var updateButtonState = function() { if(syncRunning === true || uploadPending === false || isOffline) { $(".action-force-sync").addClass("disabled"); } @@ -39,6 +39,8 @@ define([ updateButtonState(); }; + buttonSync.onReady = updateButtonState; + // Check that a file has synchronized locations var checkSynchronization = function(fileDesc) { if(_.size(fileDesc.syncLocations) !== 0) { diff --git a/js/extensions/document-selector.js b/js/extensions/document-selector.js index 4711de2f..cc69101a 100644 --- a/js/extensions/document-selector.js +++ b/js/extensions/document-selector.js @@ -45,7 +45,7 @@ define([ return fileDesc.title.toLowerCase(); }).each(function(fileDesc) { var a = $('').html(composeTitle(fileDesc)).click(function() { - if(liMap[fileDesc.fileIndex].is(".disabled")) { + if(!liMap[fileDesc.fileIndex].is(".disabled")) { fileMgr.selectFile(fileDesc); } }); diff --git a/js/extensions/manage-publication.js b/js/extensions/manage-publication.js index 054112bf..64427623 100644 --- a/js/extensions/manage-publication.js +++ b/js/extensions/manage-publication.js @@ -43,7 +43,7 @@ define([ } var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, ""); var lineElement = $(_.template(lineTemplate, { - provider: providerMap[publishAttributes.provider], + provider: publishAttributes.provider, publishDesc: publishDesc })); lineElement.append($(removeButtonTemplate).click(function() { diff --git a/js/extensions/manage-synchronization.js b/js/extensions/manage-synchronization.js index 0d236bd2..67ac87e0 100644 --- a/js/extensions/manage-synchronization.js +++ b/js/extensions/manage-synchronization.js @@ -39,7 +39,7 @@ define([ _.each(syncAttributesList, function(syncAttributes) { var syncDesc = syncAttributes.id || syncAttributes.path; var lineElement = $(_.template(lineTemplate, { - provider: providerMap[syncAttributes.provider], + provider: syncAttributes.provider, syncDesc: syncDesc })); lineElement.append($(removeButtonTemplate).click(function() { diff --git a/js/extensions/toc.js b/js/extensions/toc.js index c835c67a..d4055c2f 100644 --- a/js/extensions/toc.js +++ b/js/extensions/toc.js @@ -8,7 +8,7 @@ define([ extensionId: "toc", extensionName: "Table Of Content", optional: true, - settingsBloc: '

Generates tables of content using the marker [TOC].

' + settingsBloc: '

Generates a table of content and include it in your document using the marker [TOC].

' }; // TOC element description diff --git a/js/file-manager.js b/js/file-manager.js index 1cd93c44..53c9b8fb 100644 --- a/js/file-manager.js +++ b/js/file-manager.js @@ -194,7 +194,7 @@ define([ fileMgr.hasSync = function(provider) { return _.some(fileSystem, function(fileDesc) { return _.some(fileDesc.syncLocations, function(syncAttributes) { - syncAttributes.provider == provider.providerId; + return syncAttributes.provider === provider; }); }); }; diff --git a/js/lib/Markdown.Converter.js b/js/lib/Markdown.Converter.js index d688c4c2..865d85c1 100644 --- a/js/lib/Markdown.Converter.js +++ b/js/lib/Markdown.Converter.js @@ -204,7 +204,7 @@ else text = _UnescapeSpecialChars(text); - text = pluginHooks.postConversion(text); + text = pluginHooks.postConversion(text); // benweet // attacklab: Restore dollar signs text = text.replace(/~D/g, "$$"); @@ -939,7 +939,7 @@ else // Recursion for sub-lists: item = _DoLists(_Outdent(item), /* isInsideParagraphlessListItem= */ true); item = item.replace(/\n$/, ""); // chomp(item) - if (!isInsideParagraphlessListItem) + if (!isInsideParagraphlessListItem) // only the outer-most item should run this, otherwise it's run multiple times for the inner ones item = _RunSpanGamut(item); } last_item_had_a_double_newline = ends_with_double_newline; @@ -1231,7 +1231,12 @@ else text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback); return text; } - + + var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]", + charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]", + autoLinkRegex = new RegExp("(=\"|<)?\\b(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi"), + endCharRegex = new RegExp(charEndingUrl, "i"); + function handleTrailingParens(wholeMatch, lookbehind, protocol, link) { if (lookbehind) return wholeMatch; @@ -1258,10 +1263,16 @@ else return ""; }); } - + if (tail) { + var lastChar = link.charAt(link.length - 1); + if (!endCharRegex.test(lastChar)) { + tail = lastChar + tail; + link = link.substr(0, link.length - 1); + } + } return "<" + protocol + link + ">" + tail; } - + function _DoAutoLinks(text) { // note that at this point, all other URL in the text are already hyperlinked as
@@ -1271,7 +1282,7 @@ else // must be preceded by a non-word character (and not by =" or <) and followed by non-word/EOF character // simulating the lookbehind in a consuming way is okay here, since a URL can neither and with a " nor // with a <, so there is no risk of overlapping matches. - text = text.replace(/(="|<)?\b(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\])])(?=$|\W)/gi, handleTrailingParens); + text = text.replace(autoLinkRegex, handleTrailingParens); // autolink anything like diff --git a/js/lib/Markdown.Editor.js b/js/lib/Markdown.Editor.js index 417afcac..58c16453 100644 --- a/js/lib/Markdown.Editor.js +++ b/js/lib/Markdown.Editor.js @@ -111,20 +111,20 @@ * its own image insertion dialog, this hook should return true, and the callback should be called with the chosen * image url (or null if the user cancelled). If this hook returns false, the default dialog will be used. */ - hooks.addFalse("insertLinkDialog"); + hooks.addFalse("insertLinkDialog"); // benweet this.getConverter = function () { return markdownConverter; } var that = this, panels; - this.run = function (previewWrapper) { + this.run = function (previewWrapper) { // benweet if (panels) return; // already initialized panels = new PanelCollection(idPostfix); var commandManager = new CommandManager(hooks, getString); - var previewManager = new PreviewManager(markdownConverter, panels, function () { hooks.onPreviewRefresh(); }, previewWrapper); + var previewManager = new PreviewManager(markdownConverter, panels, function () { hooks.onPreviewRefresh(); }, previewWrapper); // benweet var undoManager, uiManager; if (!/\?noundo/.test(doc.location.href)) { @@ -821,7 +821,7 @@ this.init(); }; - function PreviewManager(converter, panels, previewRefreshCallback, previewWrapper) { + function PreviewManager(converter, panels, previewRefreshCallback, previewWrapper) { // benweet var managerObj = this; var timeout; @@ -869,7 +869,7 @@ var text = panels.input.value; - if (text !== undefined && text == oldInputText) { + if (text && text == oldInputText) { return; // Input text hasn't changed. } else { @@ -887,7 +887,7 @@ pushPreviewHtml(text); }; - if(previewWrapper !== undefined) { + if(previewWrapper !== undefined) { // benweet makePreviewHtml = previewWrapper(makePreviewHtml); } @@ -1416,12 +1416,12 @@ return false; } } - button.className = button.className.replace(/ disabled/g, ""); + button.className = button.className.replace(/ disabled/g, ""); // benweet } else { image.style.backgroundPosition = button.XShift + " " + disabledYShift; button.onmouseover = button.onmouseout = button.onclick = function () { }; - button.className += " disabled"; + button.className += " disabled"; // benweet } } @@ -1786,7 +1786,7 @@ ui.prompt(this.getString("imagedialog"), imageDefaultText, linkEnteredCallback); } else { - if (!this.hooks.insertLinkDialog(linkEnteredCallback)) + if (!this.hooks.insertLinkDialog(linkEnteredCallback)) // benweet ui.prompt(this.getString("linkdialog"), linkDefaultText, linkEnteredCallback); } return true; diff --git a/js/main.js b/js/main.js index f2087092..51fd666a 100644 --- a/js/main.js +++ b/js/main.js @@ -8,15 +8,15 @@ requirejs.config({ "lib/MathJax": '../lib/MathJax/MathJax.js?config=TeX-AMS_HTML' }, shim: { - 'lib/underscore': { + 'underscore': { exports: '_' }, - 'lib/jgrowl': { - deps: ['lib/jquery'], + 'jgrowl': { + deps: ['jquery'], exports: 'jQuery.jGrowl' }, - 'lib/jquery-ui': ['lib/jquery'], - 'lib/bootstrap': ['lib/jquery'], + 'lib/jquery-ui': ['jquery'], + 'lib/bootstrap': ['jquery'], 'lib/layout': ['lib/jquery-ui'], 'lib/Markdown.Extra': ['lib/Markdown.Converter', 'lib/prettify'], 'lib/Markdown.Editor': ['lib/Markdown.Converter'] diff --git a/js/settings.js b/js/settings.js index 5c7c9160..e94df83a 100644 --- a/js/settings.js +++ b/js/settings.js @@ -1,5 +1,6 @@ define([ - "underscore" + "underscore", + "config" ], function(_) { var settings = { diff --git a/js/synchronizer.js b/js/synchronizer.js index 0df48d99..5f43354f 100644 --- a/js/synchronizer.js +++ b/js/synchronizer.js @@ -112,7 +112,7 @@ define([ if(uploadCycle === true) { // New upload cycle uploadCycle = false; - uploadFileList = fileMgr.getFileList(); + uploadFileList = _.values(fileSystem); fileUp(callback); } else {