Skip to content

Commit 3df18d9

Browse files
authored
Merge pull request #7 from jywarren/custom-submit-method
basic test of submission form and before/after
2 parents 31983e9 + 81deb77 commit 3df18d9

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

dist/inlineMarkdownEditor.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -10346,6 +10346,7 @@ module.exports = function processSection(markdown, o) {
1034610346
o.insertEditLink(uniqueId, el, form, onEdit, false, o);
1034710347
// plan for swappable editors; will need to specify both constructor and onEditorSubmit
1034810348
function onEdit() {
10349+
var editor;
1034910350
if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) {
1035010351
// insert rich editor
1035110352
editor = new PL.Editor({
@@ -10357,25 +10358,27 @@ module.exports = function processSection(markdown, o) {
1035710358
form.hide();
1035810359
});
1035910360
form.find('button.submit').click(function(e) {
10360-
submitSectionForm(e, form, editor)
10361+
prepareAndSendSectionForm(e, form, editor, _markdown);
1036110362
});
1036210363
}
1036310364
}
10364-
10365-
function submitSectionForm(e, form, _editor) {
10366-
e.preventDefault();
10365+
10366+
function prepareAndSendSectionForm(e, form, _editor, _markdown) {
1036710367
message.html('<i class="fa fa-spinner fa-spin" style="color:#ccc;"></i>');
1036810368
if (_editor) {
1036910369
changes = _editor.richTextModule.value(); // rich editor
1037010370
} else {
1037110371
changes = form.find('textarea').val();
1037210372
}
10373-
// we should swap for a method like this:
10374-
//o.sendChanges(markdown, changes);
10375-
// but should do mocked ajax testing first
10373+
o.submitSectionForm(e, _markdown, changes, o);
10374+
}
10375+
10376+
// provide overridable default
10377+
o.submitSectionForm = o.submitSectionForm || function submitSectionForm(e, before, after, o) {
10378+
e.preventDefault();
1037610379
$.post(o.replaceUrl, {
10377-
before: markdown,
10378-
after: changes
10380+
before: before, // encodeURI(before)
10381+
after: after // encodeURI(after)
1037910382
})
1038010383
.done(function onComplete(response) {
1038110384
// we should need fewer things here:

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.1.0",
3+
"version": "0.2.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": {

spec/javascripts/replacement_spec.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
describe("Replacement functions", function() {
22

3-
var editor;
4-
53
it("won't generate an editor for an ambiguous (duplicated) section, where replacement server side could apply to the wrong section", function() {
64
fixture = loadFixtures('index.html');
7-
var html = "Unique text.\n\nDuplicated text.\n\nDuplicated text."
5+
var html = "Unique text.\n\nDuplicated text.\n\nDuplicated text.";
86
$('.markdown').html(html);
9-
editor = inlineMarkdownEditor({
7+
var editor = inlineMarkdownEditor({
108
replaceUrl: '/wiki/replace/',
119
selector: '.markdown'
1210
});
1311
expect($('.inline-edit-form textarea').length).toBe(1);
1412
});
1513

14+
it("sends exactly matching original text and 'before' parameters", function(done) {
15+
fixture = loadFixtures('index.html');
16+
var html = "## Headings [with](/links)";
17+
var new_html = "## Headings [with](/links) and other things";
18+
$('.markdown').html(html);
19+
20+
var editor = inlineMarkdownEditor({
21+
replaceUrl: '/wiki/replace/',
22+
selector: '.markdown',
23+
submitSectionForm: submitSectionForm
24+
});
25+
26+
function submitSectionForm(e, before, after, o) {
27+
expect(before).toBe(html);
28+
expect(after).toBe(new_html);
29+
expect(before).not.toBe(after);
30+
done();
31+
}
32+
33+
// generate an editor by clicking the pencil button
34+
$('.inline-edit-btn').click();
35+
36+
$('.inline-edit-form textarea').html(new_html);
37+
38+
// trigger a section save:
39+
$('.inline-edit-form:last button.submit').click();
40+
expect(editor.options.submitSectionForm).toBe(submitSectionForm);
41+
expect(editor.options.originalMarkdown).toBe(html);
42+
});
43+
1644
});

src/processSection.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function processSection(markdown, o) {
2424
o.insertEditLink(uniqueId, el, form, onEdit, false, o);
2525
// plan for swappable editors; will need to specify both constructor and onEditorSubmit
2626
function onEdit() {
27+
var editor;
2728
if (o.wysiwyg && $('#' + uniqueId).find('.wk-container').length === 0) {
2829
// insert rich editor
2930
editor = new PL.Editor({
@@ -35,25 +36,27 @@ module.exports = function processSection(markdown, o) {
3536
form.hide();
3637
});
3738
form.find('button.submit').click(function(e) {
38-
submitSectionForm(e, form, editor)
39+
prepareAndSendSectionForm(e, form, editor, _markdown);
3940
});
4041
}
4142
}
42-
43-
function submitSectionForm(e, form, _editor) {
44-
e.preventDefault();
43+
44+
function prepareAndSendSectionForm(e, form, _editor, _markdown) {
4545
message.html('<i class="fa fa-spinner fa-spin" style="color:#ccc;"></i>');
4646
if (_editor) {
4747
changes = _editor.richTextModule.value(); // rich editor
4848
} else {
4949
changes = form.find('textarea').val();
5050
}
51-
// we should swap for a method like this:
52-
//o.sendChanges(markdown, changes);
53-
// but should do mocked ajax testing first
51+
o.submitSectionForm(e, _markdown, changes, o);
52+
}
53+
54+
// provide overridable default
55+
o.submitSectionForm = o.submitSectionForm || function submitSectionForm(e, before, after, o) {
56+
e.preventDefault();
5457
$.post(o.replaceUrl, {
55-
before: markdown,
56-
after: changes
58+
before: before, // encodeURI(before)
59+
after: after // encodeURI(after)
5760
})
5861
.done(function onComplete(response) {
5962
// we should need fewer things here:

0 commit comments

Comments
 (0)