Skip to content

Commit 48a2499

Browse files
committed
A bit of a cleanup
1 parent d8122e9 commit 48a2499

File tree

8 files changed

+90
-150
lines changed

8 files changed

+90
-150
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,24 @@ wagtailembedder
33

44
Snippets embedder for Wagtail richtext fields.
55

6-
UNDER CONSTRUCTION
6+
# Quickstart
7+
8+
``` $ pip install wagtailembedder [GITHUB SSH URI]```
9+
10+
add wagtailembedder to your settings.py in the INSTALLED_APPS section:
11+
12+
```
13+
...
14+
'modelcluster',
15+
'wagtailembedder',
16+
'core',
17+
...
18+
```
19+
20+
For each of your models registered as a wagtail.wagtailsnippets create an html file to render the template inside a RichText field.
21+
Example: if we have a ```SocialMediaLink``` snippet in our ```core``` app we need to create a template in ```core/templates/snippets/social_media_link.html```
22+
23+
If no template is defined then nothing will be outputted in the frontend when rendering a RichText field, don't be a dick and write some nice templates.
24+
25+
Templates names will match snippets models names replacing capital letters with underscores, Wagtail style.
26+

wagtailembedder/helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ def expand_db_attributes(attrs, for_editor):
3535

3636

3737
def add_embed_handler():
38+
"""
39+
Add our own SnippetEmbedHandler into the wagtailcore EMBED_HANDLERS var
40+
"""
3841
EMBED_HANDLERS['snippet'] = SnippetEmbedHandler
3942
return True

wagtailembedder/static/wagtailembedder/js/hallo-embedder.js

Lines changed: 47 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -9,134 +9,65 @@
99
var button, widget;
1010

1111
widget = this;
12+
13+
getEnclosingEmbed = function() {
14+
var node;
15+
16+
node = widget.options.editable.getSelection().commonAncestorContainer;
17+
return $(node).parents('div[data-embedtype="snippet"]').get(0);
18+
};
19+
1220
button = $('<span></span>');
1321
button.hallobutton({
1422
uuid: this.options.uuid,
1523
editable: this.options.editable,
1624
label: 'Snippets',
1725
icon: 'icon-snippet',
18-
command: null
26+
queryState: function(event) {
27+
var refreshedButton = button.hallobutton('checked', getEnclosingEmbed());
28+
if ($(refreshedButton).hasClass('ui-state-active')) {
29+
$(toolbar).find('button').not($(button).children()[0]).removeClass('ui-state-active');
30+
}
31+
return refreshedButton;
32+
}
1933
});
2034
toolbar.append(button);
2135
return button.on("click", function(event) {
22-
var insertionPoint, lastSelection;
23-
24-
lastSelection = widget.options.editable.getSelection();
25-
insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
26-
return ModalWorkflow({
27-
url: window.embedderChooserUrls.embedderChooser,
28-
responses: {
29-
snippetChosen: function(embedData) {
30-
var elem;
31-
32-
elem = $(embedData).get(0);
33-
lastSelection.insertNode(elem);
34-
// if (elem.getAttribute('contenteditable') === 'false') {
35-
// insertRichTextDeleteControl(elem);
36-
// }
37-
return widget.options.editable.element.trigger('change');
36+
var enclosingLink, insertionPoint, lastSelection;
37+
38+
enclosingLink = getEnclosingEmbed();
39+
if (!enclosingLink) {
40+
41+
lastSelection = widget.options.editable.getSelection();
42+
insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
43+
return ModalWorkflow({
44+
url: window.embedderChooserUrls.embedderChooser,
45+
responses: {
46+
snippetChosen: function(embedData) {
47+
var elem = $(embedData).get(0);
48+
lastSelection.insertNode(elem);
49+
if (elem.getAttribute('contenteditable') === 'false') {
50+
insertRichTextDeleteControl(elem);
51+
}
52+
53+
var parentElement = $(lastSelection.endContainer);
54+
var eol;
55+
if ($(parentElement).hasClass('richtext')) {
56+
eol = $(elem).parent().append("<br/>");
57+
} else {
58+
parentElement = $(parentElement).parent();
59+
eol = $( "<br/>" ).insertAfter(parentElement);
60+
}
61+
placeCaretAtEnd(eol[0]);
62+
63+
return widget.options.editable.element.trigger('change');
64+
}
3865
}
39-
}
40-
});
66+
});
67+
}
4168
});
4269
}
4370
});
4471
})(jQuery);
4572

46-
}).call(this);
47-
48-
49-
50-
51-
52-
53-
// (function() {
54-
// (function($) {
55-
// return $.widget("IKS.halloembedder", {
56-
// options: {
57-
// uuid: '',
58-
// editable: null
59-
// },
60-
// populateToolbar: function(toolbar) {
61-
// var button, getEnclosingLink, widget;
62-
63-
// widget = this;
64-
// getEnclosingLink = function() {
65-
// var node;
66-
67-
// node = widget.options.editable.getSelection().commonAncestorContainer;
68-
// return $(node).parents('a').get(0);
69-
// };
70-
// button = $('<span></span>');
71-
// button.hallobutton({
72-
// uuid: this.options.uuid,
73-
// editable: this.options.editable,
74-
// label: 'Snippets',
75-
// icon: 'icon-snippet',
76-
// command: null
77-
// /*queryState: function(event) {
78-
// return button.hallobutton('checked', !!getEnclosingLink());
79-
// }*/
80-
// });
81-
// toolbar.append(button);
82-
// return button.on("click", function(event) {
83-
// var insertionPoint, lastSelection;
84-
85-
// lastSelection = widget.options.editable.getSelection();
86-
// insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
87-
88-
// return ModalWorkflow({
89-
// url: window.embedderChooserUrls.embedderChooser,
90-
// responses: {
91-
// embedChosen: function(embedData) {
92-
// var elem;
93-
// elem = $(embedData).get(0);
94-
// lastSelection.insertNode(elem);
95-
// // if (elem.getAttribute('contenteditable') === 'false') {
96-
// // insertRichTextDeleteControl(elem);
97-
// // }
98-
// return widget.options.editable.element.trigger('change');
99-
// }
100-
// }
101-
// });
102-
// // enclosingLink = getEnclosingLink();
103-
// /*if (enclosingLink) {
104-
// $(enclosingLink).replaceWith(enclosingLink.innerHTML);
105-
// button.hallobutton('checked', false);
106-
// return widget.options.editable.element.trigger('change');
107-
// } else {
108-
// lastSelection = widget.options.editable.getSelection();
109-
// if (lastSelection.collapsed) {
110-
// url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true&prompt_for_link_text=true';
111-
// } else {
112-
// url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true';
113-
// }
114-
// return ModalWorkflow({
115-
// url: url,
116-
// responses: {
117-
// pageChosen: function(pageData) {
118-
// var a;
119-
120-
// a = document.createElement('a');
121-
// a.setAttribute('href', pageData.url);
122-
// if (pageData.id) {
123-
// a.setAttribute('data-id', pageData.id);
124-
// a.setAttribute('data-linktype', 'page');
125-
// }
126-
// if ((!lastSelection.collapsed) && lastSelection.canSurroundContents()) {
127-
// lastSelection.surroundContents(a);
128-
// } else {
129-
// a.appendChild(document.createTextNode(pageData.title));
130-
// lastSelection.insertNode(a);
131-
// }
132-
// return widget.options.editable.element.trigger('change');
133-
// }
134-
// }
135-
// });
136-
// }*/
137-
// });
138-
// }
139-
// });
140-
// })(jQuery);
141-
142-
// }).call(this);
73+
}).call(this);

wagtailembedder/templates/wagtailembedder/snippets/type_index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ <h2><a class="snippet-choice" href="{% url 'wagtailembedder_choose_snippet' snip
1313
</li>
1414
{% endfor %}
1515
{% else %}
16+
{% url 'wagtailsnippets_create' content_type.app_label content_type.model as wagtailsnippets_create_url %}
1617
<li>
17-
<p>No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one first?</a>?</p>
18+
<p>No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one first</a>?</p>
1819
</li>
1920
{% endif %}
2021
</ul>

wagtailembedder/templates/wagtailembedder/snippets/type_index.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ function(modal) {
22
$('div.snippet-list h2 a.snippet-choice').click(function(event){
33
event.preventDefault();
44
modal.loadUrl(this.href);
5-
// $.get($(this).attr('href'), function(response) {
6-
7-
// })
8-
// .done(function() {
9-
// console.log( "second success" );
10-
// })
11-
// .fail(function(error) {
12-
// console.log(error);
13-
// console.log( "error" );
14-
// });
15-
165
return false;
176
});
187
}

wagtailembedder/templates/wagtailembedder/snippets/types_list.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ function(modal) {
22
$('div.snippet-list h2 a.snippet-embedder').click(function(event){
33
event.preventDefault();
44
modal.loadUrl(this.href);
5-
// $.get($(this).attr('href'), function(response) {
6-
7-
// })
8-
// .done(function() {
9-
// console.log( "second success" );
10-
// })
11-
// .fail(function(error) {
12-
// console.log(error);
13-
// console.log( "error" );
14-
// });
15-
165
return false;
176
});
187
}

wagtailembedder/views/chooser.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
@permission_required('wagtailadmin.access_admin')
1616
def index(request):
17-
snippet_types = [
18-
(
19-
get_snippet_type_name(content_type)[1],
20-
get_snippet_type_description(content_type),
21-
content_type
22-
)
17+
snippet_types = [(
18+
get_snippet_type_name(content_type)[1],
19+
get_snippet_type_description(content_type),
20+
content_type)
2321
for content_type in get_snippet_content_types()
2422
if user_can_edit_snippet_type(request.user, content_type)
2523
]
@@ -41,6 +39,7 @@ def index_objects(request, content_type_app_name, content_type_model_name):
4139
model = content_type.model_class()
4240
items = model.objects.all()
4341
snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)
42+
4443
return render_modal_workflow(
4544
request,
4645
'wagtailembedder/snippets/type_index.html',
@@ -62,14 +61,19 @@ def choose_snippet(request, id, content_type_app_name, content_type_model_name):
6261
raise PermissionDenied
6362
model = content_type.model_class()
6463
try:
65-
instance = model.objects.get(id=id)
64+
model.objects.get(id=id)
6665
except ObjectDoesNotExist:
6766
return render_modal_workflow(
68-
request, None, None,
67+
request,
68+
None,
69+
None,
6970
{'error': 'Sorry, an error occurred. Contact support or try again later.'}
7071
)
7172
embed_html = embed_to_editor_html(id, content_type_app_name, content_type_model_name)
73+
7274
return render_modal_workflow(
73-
request, None, 'wagtailembedder/editor/snippet_chosen.js',
75+
request,
76+
None,
77+
'wagtailembedder/editor/snippet_chosen.js',
7478
{'embed_html': embed_html}
7579
)

wagtailembedder/wagtail_hooks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def register_admin_urls():
1818
@hooks.register('insert_editor_js')
1919
def editor_js():
2020
"""
21-
Add extra JS files to the admin
21+
Add extra JS files to the admin to register the plugin and set the reversed URL in JS vars
2222
"""
2323
js_files = [
2424
'wagtailembedder/js/hallo-embedder.js',
@@ -38,4 +38,7 @@ def editor_js():
3838

3939
@hooks.register('before_serve_page')
4040
def add_handler(page, request, serve_args, serve_kwargs):
41+
"""
42+
Call add_embed_handler() to set a custom handler for embedded snippets
43+
"""
4144
add_embed_handler()

0 commit comments

Comments
 (0)