2023-02-20 11:00:31 +00:00
|
|
|
from django.urls import include, path, reverse
|
2020-01-08 04:41:21 +00:00
|
|
|
from django.utils.html import format_html
|
2022-05-26 16:03:49 +01:00
|
|
|
from django.utils.translation import gettext
|
2023-02-20 11:00:31 +00:00
|
|
|
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
|
2023-02-20 11:00:31 +00:00
|
|
|
from .richtext import (ContentstateSnippetEmbedConversionRule,
|
|
|
|
ContentstateSnippetLinkConversionRule,
|
|
|
|
SnippetEmbedHandler, SnippetLinkHandler)
|
2020-01-08 04:41:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hooks.register("register_rich_text_features")
|
2020-04-03 15:32:17 +01:00
|
|
|
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(
|
2022-05-26 16:03:49 +01:00
|
|
|
{"type": type_, "icon": "snippet", "description": gettext("Snippet Link")},
|
2020-01-08 04:41:21 +00:00
|
|
|
js=[
|
2023-02-20 11:00:31 +00:00
|
|
|
"wagtailsnippets/js/snippet-chooser.js",
|
2020-01-08 04:41:21 +00:00
|
|
|
"wagtail_draftail_snippet/js/snippet-model-chooser-modal.js",
|
2020-04-03 15:59:55 +01:00
|
|
|
"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(
|
2022-05-26 16:03:49 +01:00
|
|
|
{"type": type_, "icon": "code", "description": gettext("Snippet Embed")},
|
2020-03-31 17:34:05 +01:00
|
|
|
js=[
|
2023-02-20 11:00:31 +00:00
|
|
|
"wagtailsnippets/js/snippet-chooser.js",
|
2020-03-31 17:34:05 +01:00
|
|
|
"wagtail_draftail_snippet/js/snippet-model-chooser-modal.js",
|
2020-04-03 15:59:55 +01:00
|
|
|
"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():
|
2022-02-17 17:09:05 +00:00
|
|
|
|
|
|
|
html = f"""
|
2020-04-03 15:19:38 +01:00
|
|
|
<script>
|
2022-02-17 17:09:05 +00:00
|
|
|
window.chooserUrls.snippetChooser = '{reverse('wagtaildraftailsnippet:choose_generic')}';
|
|
|
|
window.chooserUrls.snippetLinkModelChooser = '{reverse("wagtaildraftailsnippet:choose-snippet-link-model")}';
|
|
|
|
window.chooserUrls.snippetEmbedModelChooser = '{reverse("wagtaildraftailsnippet:choose-snippet-embed-model")}';
|
|
|
|
</script>
|
|
|
|
"""
|
|
|
|
|
|
|
|
return format_html(html)
|
2020-01-08 04:41:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hooks.register("register_admin_urls")
|
|
|
|
def register_admin_urls():
|
2022-05-26 16:03:49 +01:00
|
|
|
return [path("snippets/", include(urls, namespace="wagtaildraftailsnippet"))]
|