diff --git a/README.md b/README.md index 08e039a..ac12f7a 100644 --- a/README.md +++ b/README.md @@ -37,19 +37,19 @@ inlineMarkdownEditor({ replaceUrl: '/wiki/replace/' + wiki_id, selector: '.markdown', preProcessor: function preProcessMarkdown(markdown) { - // do things to markdown here + // do things to markdown here before it's used to construct the form return markdown }, postProcessor: function postProcessContent(element) { - // do things to element here + // do things to element here after the section has been converted to HTML and displayed }, defaultMarkdown: function(markdown) {}, // a markdown parser buildSectionForm: function() {}, // return a string containing the form element onComplete: function(response, markdown, html, el, uniqueId, form, o) {}, // run on completing AJAX post isEditable: function(markdown) {}, // returns boolean; whether a given subsection should get an inline form; default skips HTML and horizontal rules extraButtons: { 'fa-icon-name': function(element) {} }, // object with keys of icon names for additional buttons with associated actions for each; returns jQuery element upon construction - submitSectionForm: function(event, before, after, options) {} // optional, to override the form submission handling for each subsection; before/after represent the text diff - + submitSectionForm: function(event, before, after, options) {}, // optional, to override the form submission handling for each subsection; before/after represent the text diff + editorOptions: {} // any options to pass to the built-in PublicLab.Editor instance }); ``` diff --git a/dist/inlineMarkdownEditor.js b/dist/inlineMarkdownEditor.js index f7225ef..867ba91 100644 --- a/dist/inlineMarkdownEditor.js +++ b/dist/inlineMarkdownEditor.js @@ -10237,6 +10237,7 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) { o.processSections = require('./processSections.js'); var el = $(o.selector); o.originalMarkdown = el.html(); + o.preProcessor = o.preProcessor || function(m) { return m; } // split by double-newline: var sections = o.originalMarkdown .replace(/\n[\n]+/g, "\n\n") @@ -10244,7 +10245,7 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) { var editableSections = []; // we also do this inside processSection, but independently track here: sections.forEach(function forEachSection(section, index) { - if (o.isEditable(section, o.originalMarkdown)) editableSections.push(section); + if (o.isEditable(section, o.preProcessor(o.originalMarkdown))) editableSections.push(section); }); el.html(''); // we might start running processSections only on editableSections... @@ -10299,8 +10300,8 @@ module.exports = function isEditable(markdown, originalMarkdown) { },{}],99:[function(require,module,exports){ module.exports = function onComplete(response, markdown, html, el, uniqueId, form, o) { + var message = form.find('.section-message'); if (response === 'true' || response === true) { - var message = $('#' + uniqueId + ' .section-message'); message.html(''); //markdown = changes; $('#' + uniqueId + ' textarea').val(''); @@ -10328,7 +10329,8 @@ module.exports = function processSection(markdown, o) { uniqueId = "section-form-" + randomNum, filteredMarkdown = markdown; - if (o.preProcessor) filteredMarkdown = o.preProcessor(markdown); + var originalSectionMarkdown = markdown; + filteredMarkdown = o.preProcessor(markdown); html = o.defaultMarkdown(filteredMarkdown); $(o.selector).append('
'); @@ -10341,7 +10343,7 @@ module.exports = function processSection(markdown, o) { var message = $('#' + uniqueId + ' .section-message'); function insertFormIfMarkdown(_markdown, el, uniqueId) { - if (o.isEditable(_markdown, o.originalMarkdown)) { + if (o.isEditable(_markdown, o.preProcessor(o.originalMarkdown))) { var formHtml = o.buildSectionForm(uniqueId, _markdown); el.after(formHtml); var _form = $('#' + uniqueId); @@ -10351,16 +10353,16 @@ module.exports = function processSection(markdown, o) { var editor; if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) { // insert rich editor - editor = new PL.Editor({ - textarea: $('#' + uniqueId + ' textarea')[0] - }); + var editorOptions = o.editorOptions || {}; + editorOptions.textarea = $('#' + uniqueId + ' textarea')[0]; + editor = new PL.Editor(editorOptions); } _form.find('.cancel').click(function inlineEditCancelClick(e) { e.preventDefault(); _form.hide(); }); _form.find('button.submit').click(function(e) { - prepareAndSendSectionForm(e, _form, editor, _markdown); + prepareAndSendSectionForm(e, _form, editor, originalSectionMarkdown); }); } } diff --git a/package.json b/package.json index dd2ddfb..3d970bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "inline-markdown-editor", - "version": "0.2.3", + "version": "0.3.0", "description": "An inline wysiwyg markdown document editor based on replacing string subsections. WYSIWYG possible via woofmark.", "main": "dist/inlineMarkdownEditor.js", "scripts": { diff --git a/src/inlineMarkdownEditor.js b/src/inlineMarkdownEditor.js index 95501a4..17771ef 100644 --- a/src/inlineMarkdownEditor.js +++ b/src/inlineMarkdownEditor.js @@ -8,6 +8,7 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) { o.processSections = require('./processSections.js'); var el = $(o.selector); o.originalMarkdown = el.html(); + o.preProcessor = o.preProcessor || function(m) { return m; } // split by double-newline: var sections = o.originalMarkdown .replace(/\n[\n]+/g, "\n\n") @@ -15,7 +16,7 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) { var editableSections = []; // we also do this inside processSection, but independently track here: sections.forEach(function forEachSection(section, index) { - if (o.isEditable(section, o.originalMarkdown)) editableSections.push(section); + if (o.isEditable(section, o.preProcessor(o.originalMarkdown))) editableSections.push(section); }); el.html(''); // we might start running processSections only on editableSections... diff --git a/src/onComplete.js b/src/onComplete.js index 56756c5..1502866 100644 --- a/src/onComplete.js +++ b/src/onComplete.js @@ -1,6 +1,6 @@ module.exports = function onComplete(response, markdown, html, el, uniqueId, form, o) { + var message = form.find('.section-message'); if (response === 'true' || response === true) { - var message = $('#' + uniqueId + ' .section-message'); message.html(''); //markdown = changes; $('#' + uniqueId + ' textarea').val(''); diff --git a/src/processSection.js b/src/processSection.js index 436090c..7e4f302 100644 --- a/src/processSection.js +++ b/src/processSection.js @@ -4,7 +4,8 @@ module.exports = function processSection(markdown, o) { uniqueId = "section-form-" + randomNum, filteredMarkdown = markdown; - if (o.preProcessor) filteredMarkdown = o.preProcessor(markdown); + var originalSectionMarkdown = markdown; + filteredMarkdown = o.preProcessor(markdown); html = o.defaultMarkdown(filteredMarkdown); $(o.selector).append(''); @@ -17,7 +18,7 @@ module.exports = function processSection(markdown, o) { var message = $('#' + uniqueId + ' .section-message'); function insertFormIfMarkdown(_markdown, el, uniqueId) { - if (o.isEditable(_markdown, o.originalMarkdown)) { + if (o.isEditable(_markdown, o.preProcessor(o.originalMarkdown))) { var formHtml = o.buildSectionForm(uniqueId, _markdown); el.after(formHtml); var _form = $('#' + uniqueId); @@ -27,16 +28,16 @@ module.exports = function processSection(markdown, o) { var editor; if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) { // insert rich editor - editor = new PL.Editor({ - textarea: $('#' + uniqueId + ' textarea')[0] - }); + var editorOptions = o.editorOptions || {}; + editorOptions.textarea = $('#' + uniqueId + ' textarea')[0]; + editor = new PL.Editor(editorOptions); } _form.find('.cancel').click(function inlineEditCancelClick(e) { e.preventDefault(); _form.hide(); }); _form.find('button.submit').click(function(e) { - prepareAndSendSectionForm(e, _form, editor, _markdown); + prepareAndSendSectionForm(e, _form, editor, originalSectionMarkdown); }); } }