Skip to content

Commit 8da3106

Browse files
authored
Merge pull request #3 from jywarren/additional-buttons
Additional buttons
2 parents 6e377dc + 6b75cd2 commit 8da3106

9 files changed

+80
-16
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ inlineMarkdownEditor({
4646
defaultMarkdown: function(markdown) {}, // a markdown parser
4747
buildSectionForm: function() {}, // return a string containing the form element
4848
onComplete: function(response, markdown, html, el, uniqueId, form, o) {}, // run on completing AJAX post
49-
isEditable: function(markdown) {} // returns boolean; whether a given subsection should get an inline form; default skips HTML and horizontal rules
49+
isEditable: function(markdown) {}, // returns boolean; whether a given subsection should get an inline form; default skips HTML and horizontal rules
50+
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
5051

5152
});
5253
```
@@ -68,6 +69,7 @@ Tests are set up with Jasmine, and can be run with `npm test`.
6869
## Goals
6970

7071
* configurable editors
72+
* plan for swappable editors; will need to specify both constructor and onEditorSubmit in processSection
7173
* better modularization of processSection.js
7274
* more tests
7375

dist/inlineMarkdownEditor.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -10239,27 +10239,43 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) {
1023910239
o.originalMarkdown = el.html();
1024010240
// split by double-newline:
1024110241
var sections = o.originalMarkdown.split('\n\n');
10242+
var editableSections = [];
10243+
// we also do this inside processSection, but independently track here:
10244+
sections.forEach(function forEachSection(section, index) {
10245+
if (o.isEditable(section, o.originalMarkdown)) editableSections.push(section);
10246+
});
1024210247
el.html('');
10248+
// we might start running processSections only on editableSections...
1024310249
o.processSections(sections, o);
1024410250
el.show();
1024510251
return {
1024610252
element: el,
1024710253
sections: sections,
10254+
editableSections: editableSections,
1024810255
options: o
1024910256
};
1025010257
}
1025110258
module.exports = inlineMarkdownEditor;
1025210259

1025310260
},{"./buildSectionForm.js":94,"./defaultMarkdown.js":95,"./insertEditLink.js":97,"./isEditable.js":98,"./onComplete.js":99,"./onFail.js":100,"./processSections.js":102}],97:[function(require,module,exports){
10254-
module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor) {
10261+
module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor, o) {
1025510262
var editBtns = "";
1025610263
editBtns += "<span class='inline-edit-btns inline-edit-btns-" + uniqueId + "'>";
10257-
editBtns += "<a class='inline-edit-btn inline-edit-link inline-edit-link-" + uniqueId + "'><i class='fa fa-pencil'></i></a>";
10258-
// editBtns += "<a class='inline-edit-btn inline-edit-image inline-edit-image-" + uniqueId + "'><i class='fa fa-image'></i></a>";
10259-
editBtns += "<i>Edit</i>";
10264+
editBtns += "<a class='inline-edit-btn inline-edit-btn-editor-" + uniqueId + " inline-edit-btn-" + uniqueId + "'><i class='fa fa-pencil'></i></a>";
10265+
if (o.extraButtons) {
10266+
Object.keys(o.extraButtons).forEach(function(key, index) {
10267+
editBtns += "<a class='inline-edit-btn inline-edit-btn-" + key + " inline-edit-btn-" + uniqueId + " inline-edit-btn-" + uniqueId + "-" + key + "'><i class='fa " + key + "'></i></a>";
10268+
});
10269+
}
1026010270
editBtns += "</span>";
1026110271
el.append(editBtns);
10262-
$('.inline-edit-link-' + uniqueId).click(function inlineEditLinkClick(e) {
10272+
if (o.extraButtons) {
10273+
Object.keys(o.extraButtons).forEach(function(key, index) {
10274+
// run respective functions and pass in the elements
10275+
o.extraButtons[key]($('.inline-edit-btn-' + uniqueId + '-' + key), uniqueId);
10276+
});
10277+
}
10278+
$('.inline-edit-btn-editor-' + uniqueId).click(function inlineEditLinkClick(e) {
1026310279
e.preventDefault();
1026410280
form.toggle();
1026510281
if (onEdit) onEdit(); // callback
@@ -10290,7 +10306,7 @@ module.exports = function onComplete(response, markdown, html, el, uniqueId, for
1029010306
// replace the section but reset our html and markdown
1029110307
html = o.defaultMarkdown(markdown);
1029210308
el.html(html);
10293-
o.insertEditLink(uniqueId, el, form);
10309+
o.insertEditLink(uniqueId, el, form, false, false, o);
1029410310
if (o.postProcessor) o.postProcessor(el); // add #hashtag and @callout links, extra CSS and deep links
1029510311
} else {
1029610312
message.html('<b style="color:#a33">There was an error</b> -- the wiki page may have changed while you were editing; save your content in the clipboard and try refreshing the page.');
@@ -10327,7 +10343,8 @@ module.exports = function processSection(markdown, o) {
1032710343
var formHtml = o.buildSectionForm(uniqueId, _markdown);
1032810344
el.after(formHtml);
1032910345
var form = $('#' + uniqueId);
10330-
o.insertEditLink(uniqueId, el, form, onEdit);
10346+
o.insertEditLink(uniqueId, el, form, onEdit, false, o);
10347+
// plan for swappable editors; will need to specify both constructor and onEditorSubmit
1033110348
function onEdit() {
1033210349
if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) {
1033310350
// insert rich editor

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "inline-markdown-editor",
3-
"version": "0.0.5",
3+
"version": "0.1.0",
44
"description": "An inline wysiwyg markdown document editor based on replacing string subsections. WYSIWYG possible via woofmark.",
55
"main": "dist/inlineMarkdownEditor.js",
66
"scripts": {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
describe("Customization functions", function() {
2+
3+
var editor;
4+
5+
it("adds new buttons and runs their setup functions", function() {
6+
var fixture = loadFixtures('index.html');
7+
var buttonSetupRunCount = 0;
8+
editor = inlineMarkdownEditor({
9+
replaceUrl: '/wiki/replace/',
10+
selector: '.markdown',
11+
extraButtons: {
12+
// here we specify a button icon and a function to be run on it
13+
'fa-book': function exampleBookFunction(element, uniqueId) {
14+
expect(element).not.toBeUndefined();
15+
expect(element.length).toBe(1);
16+
expect($('.inline-edit-btn-' + uniqueId).length).toBe(2); // two because there are two buttons per section
17+
expect($('.inline-edit-btn-' + uniqueId + '-fa-book').length).toBe(1);
18+
buttonSetupRunCount += 1;
19+
}
20+
}
21+
});
22+
expect(buttonSetupRunCount).toBe(editor.editableSections.length);
23+
expect($('.inline-edit-btns a i.fa-book').length).toBe(editor.editableSections.length);
24+
expect($('.inline-edit-btn-fa-book').length).toBe(editor.editableSections.length);
25+
});
26+
27+
});

spec/javascripts/editor_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe("Editor", function() {
3535
expect(editor.sections.length).toBe(6);
3636
expect($('.inline-section').length).toBe(6);
3737
expect(editor.sections[0]).toBe('');
38+
expect(editor.editableSections.length).toBe(3);
3839
expect($('.inline-edit-form').length).toBe(3);
3940
expect($('.inline-edit-form textarea').length).toBe(3);
4041
});

src/inlineMarkdownEditor.js

+7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) {
1010
o.originalMarkdown = el.html();
1111
// split by double-newline:
1212
var sections = o.originalMarkdown.split('\n\n');
13+
var editableSections = [];
14+
// we also do this inside processSection, but independently track here:
15+
sections.forEach(function forEachSection(section, index) {
16+
if (o.isEditable(section, o.originalMarkdown)) editableSections.push(section);
17+
});
1318
el.html('');
19+
// we might start running processSections only on editableSections...
1420
o.processSections(sections, o);
1521
el.show();
1622
return {
1723
element: el,
1824
sections: sections,
25+
editableSections: editableSections,
1926
options: o
2027
};
2128
}

src/insertEditLink.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor) {
1+
module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor, o) {
22
var editBtns = "";
33
editBtns += "<span class='inline-edit-btns inline-edit-btns-" + uniqueId + "'>";
4-
editBtns += "<a class='inline-edit-btn inline-edit-link inline-edit-link-" + uniqueId + "'><i class='fa fa-pencil'></i></a>";
5-
// editBtns += "<a class='inline-edit-btn inline-edit-image inline-edit-image-" + uniqueId + "'><i class='fa fa-image'></i></a>";
6-
editBtns += "<i>Edit</i>";
4+
editBtns += "<a class='inline-edit-btn inline-edit-btn-editor-" + uniqueId + " inline-edit-btn-" + uniqueId + "'><i class='fa fa-pencil'></i></a>";
5+
if (o.extraButtons) {
6+
Object.keys(o.extraButtons).forEach(function(key, index) {
7+
editBtns += "<a class='inline-edit-btn inline-edit-btn-" + key + " inline-edit-btn-" + uniqueId + " inline-edit-btn-" + uniqueId + "-" + key + "'><i class='fa " + key + "'></i></a>";
8+
});
9+
}
710
editBtns += "</span>";
811
el.append(editBtns);
9-
$('.inline-edit-link-' + uniqueId).click(function inlineEditLinkClick(e) {
12+
if (o.extraButtons) {
13+
Object.keys(o.extraButtons).forEach(function(key, index) {
14+
// run respective functions and pass in the elements
15+
o.extraButtons[key]($('.inline-edit-btn-' + uniqueId + '-' + key), uniqueId);
16+
});
17+
}
18+
$('.inline-edit-btn-editor-' + uniqueId).click(function inlineEditLinkClick(e) {
1019
e.preventDefault();
1120
form.toggle();
1221
if (onEdit) onEdit(); // callback

src/onComplete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function onComplete(response, markdown, html, el, uniqueId, for
88
// replace the section but reset our html and markdown
99
html = o.defaultMarkdown(markdown);
1010
el.html(html);
11-
o.insertEditLink(uniqueId, el, form);
11+
o.insertEditLink(uniqueId, el, form, false, false, o);
1212
if (o.postProcessor) o.postProcessor(el); // add #hashtag and @callout links, extra CSS and deep links
1313
} else {
1414
message.html('<b style="color:#a33">There was an error</b> -- the wiki page may have changed while you were editing; save your content in the clipboard and try refreshing the page.');

src/processSection.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = function processSection(markdown, o) {
2121
var formHtml = o.buildSectionForm(uniqueId, _markdown);
2222
el.after(formHtml);
2323
var form = $('#' + uniqueId);
24-
o.insertEditLink(uniqueId, el, form, onEdit);
24+
o.insertEditLink(uniqueId, el, form, onEdit, false, o);
25+
// plan for swappable editors; will need to specify both constructor and onEditorSubmit
2526
function onEdit() {
2627
if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) {
2728
// insert rich editor

0 commit comments

Comments
 (0)