1
Fork 0
This repository has been archived on 2024-07-16. You can view files and clone it, but cannot push or open issues or pull requests.
wagtail-draftail-snippet/wagtail_draftail_snippet/wagtail_hooks.py

85 lines
2.7 KiB
Python
Raw Normal View History

2020-01-08 04:41:21 +00:00
from django.conf.urls import include, url
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext
from wagtail.admin.rich_text.editors.draftail import features as draftail_features
2020-01-08 04:41:21 +00:00
from wagtail.core import hooks
from . import urls
2020-03-31 17:34:05 +01:00
from .richtext import (
ContentstateSnippetLinkConversionRule, ContentstateSnippetEmbedConversionRule,
SnippetLinkHandler, SnippetEmbedHandler,
2020-03-31 17:34:05 +01:00
)
2020-01-08 04:41:21 +00:00
@hooks.register("register_rich_text_features")
def register_snippet_link_feature(features):
feature_name = "snippet-link"
2020-01-08 04:41:21 +00:00
type_ = "SNIPPET"
features.register_link_type(SnippetLinkHandler)
features.register_editor_plugin(
"draftail",
feature_name,
draftail_features.EntityFeature(
{"type": type_, "icon": "snippet", "description": ugettext("Snippet Link")},
2020-01-08 04:41:21 +00:00
js=[
"wagtailsnippets/js/snippet-chooser-modal.js",
"wagtail_draftail_snippet/js/snippet-model-chooser-modal.js",
"wagtail_draftail_snippet/js/wagtail-draftail-snippet.js",
2020-01-08 04:41:21 +00:00
],
),
)
features.register_converter_rule(
"contentstate", feature_name, ContentstateSnippetLinkConversionRule
)
2020-03-31 17:34:05 +01:00
@hooks.register("register_rich_text_features")
def register_snippet_embed_feature(features):
feature_name = "snippet-embed"
type_ = "SNIPPET-EMBED"
features.register_embed_type(SnippetEmbedHandler)
features.register_editor_plugin(
"draftail",
feature_name,
draftail_features.EntityFeature(
{"type": type_, "icon": "code", "description": ugettext("Snippet Embed")},
2020-03-31 17:34:05 +01:00
js=[
"wagtailsnippets/js/snippet-chooser-modal.js",
"wagtail_draftail_snippet/js/snippet-model-chooser-modal.js",
"wagtail_draftail_snippet/js/wagtail-draftail-snippet.js",
2020-03-31 17:34:05 +01:00
],
),
)
features.register_converter_rule(
"contentstate", feature_name, ContentstateSnippetEmbedConversionRule
)
2020-01-08 04:41:21 +00:00
@hooks.register("insert_editor_js")
def editor_js():
return format_html(
"""
<script>
window.chooserUrls.snippetChooser = '{0}';
window.chooserUrls.snippetLinkModelChooser = '{1}';
window.chooserUrls.snippetEmbedModelChooser = '{2}';
</script>
2020-01-08 04:41:21 +00:00
""",
reverse('wagtailsnippets:choose_generic'),
reverse("wagtaildraftailsnippet:choose-snippet-link-model"),
reverse("wagtaildraftailsnippet:choose-snippet-embed-model"),
2020-01-08 04:41:21 +00:00
)
@hooks.register("register_admin_urls")
def register_admin_urls():
return [url(r"^snippets/", include(urls, namespace="wagtaildraftailsnippet"))]