From d72571d4932acb1b159446d90fb43707858ef844 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Tue, 17 Dec 2024 13:26:44 +0100 Subject: [PATCH] Back to configuring presets, sorry --- README.rst | 8 +++++--- .../{init.js => default.js} | 0 django_prose_editor/widgets.py | 20 +++++++++++++------ esbuild.mjs | 2 +- src/{init.js => default.js} | 0 5 files changed, 20 insertions(+), 10 deletions(-) rename django_prose_editor/static/django_prose_editor/{init.js => default.js} (100%) rename src/{init.js => default.js} (100%) diff --git a/README.rst b/README.rst index 225d6c9..c2c52e2 100644 --- a/README.rst +++ b/README.rst @@ -101,9 +101,11 @@ additional assets to load: from js_asset import JS - DJANGO_PROSE_EDITOR_ASSETS = [ - JS("prose-editors/announcements.js", {"defer": True}), - ] + DJANGO_PROSE_EDITOR_PRESETS = { + "announcements": [ + JS("prose-editors/announcements.js", {"defer": True}), + ], + } The preset can be selected when instantiating the field: diff --git a/django_prose_editor/static/django_prose_editor/init.js b/django_prose_editor/static/django_prose_editor/default.js similarity index 100% rename from django_prose_editor/static/django_prose_editor/init.js rename to django_prose_editor/static/django_prose_editor/default.js diff --git a/django_prose_editor/widgets.py b/django_prose_editor/widgets.py index d11c2a7..fe0a11e 100644 --- a/django_prose_editor/widgets.py +++ b/django_prose_editor/widgets.py @@ -23,7 +23,6 @@ def base_media(self): ] }, js=[ - JS("django_prose_editor/editor.js", {"defer": True}), JSON( { "stylesheets": [ @@ -39,18 +38,27 @@ def base_media(self): }, "django-prose-editor-settings", ), - JS("django_prose_editor/init.js", {"defer": True}), + JS("django_prose_editor/editor.js", {"defer": True}), ], ) @property def media(self): - return ( - (self.base_media + forms.Media(js=assets)) - if (assets := getattr(settings, "DJANGO_PROSE_EDITOR_ASSETS", ())) - else self.base_media + return self.base_media + forms.Media( + js=[ + JS("django_prose_editor/editor.js", {"defer": True}), + *self.get_presets()[self.preset], + ] ) + def get_presets(self): + return { + "default": [ + JS("django_prose_editor/editor.js", {"defer": True}), + JS("django_prose_editor/default.js", {"defer": True}), + ], + } | getattr(settings, "DJANGO_PROSE_EDITOR_PRESETS", {}) + def get_config(self): return self.config or { "types": None, diff --git a/esbuild.mjs b/esbuild.mjs index 216d8a1..60a1103 100644 --- a/esbuild.mjs +++ b/esbuild.mjs @@ -3,7 +3,7 @@ import postcss from "esbuild-postcss" const devMode = process.argv.includes("watch") const ctx = await esbuild.context({ - entryPoints: ["./src/init.js", "./src/overrides.css", "./src/editor.js"], + entryPoints: ["./src/default.js", "./src/overrides.css", "./src/editor.js"], minify: true, bundle: true, target: "es6", diff --git a/src/init.js b/src/default.js similarity index 100% rename from src/init.js rename to src/default.js