forked from agusmakmun/django-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
142 lines (119 loc) · 5.04 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import random
import string
from django import forms
from django.contrib.admin import widgets
from django.template.loader import get_template
from django.urls import reverse
from .settings import (
MARTOR_ALTERNATIVE_CSS_FILE_THEME,
MARTOR_ALTERNATIVE_JQUERY_JS_FILE,
MARTOR_ALTERNATIVE_JS_FILE_THEME,
MARTOR_ENABLE_ADMIN_CSS,
MARTOR_ENABLE_CONFIGS,
MARTOR_MARKDOWN_BASE_EMOJI_URL,
MARTOR_MARKDOWNIFY_TIMEOUT,
MARTOR_SEARCH_USERS_URL,
MARTOR_THEME,
MARTOR_TOOLBAR_BUTTONS,
MARTOR_UPLOAD_URL,
)
def get_theme():
"""function to get the selected theme"""
supported_themes = ["bootstrap", "semantic"]
if MARTOR_THEME in supported_themes:
return MARTOR_THEME
return "bootstrap"
class MartorWidget(forms.Textarea):
def render(self, name, value, attrs=None, renderer=None, **kwargs):
# Create random string to make field ID unique to prevent duplicated ID
# when rendering fields with the same field name
random_string = "".join(
random.choice(string.ascii_letters + string.digits) for x in range(10)
)
attrs["id"] = attrs["id"] + "-" + random_string
# Make the settings the default attributes to pass
attributes_to_pass = {
"data-enable-configs": MARTOR_ENABLE_CONFIGS,
"data-markdownfy-url": reverse("martor_markdownfy"),
}
if MARTOR_UPLOAD_URL:
attributes_to_pass["data-upload-url"] = MARTOR_UPLOAD_URL
if MARTOR_SEARCH_USERS_URL:
attributes_to_pass["data-search-users-url"] = MARTOR_SEARCH_USERS_URL
if MARTOR_SEARCH_USERS_URL:
attributes_to_pass["data-base-emoji-url"] = MARTOR_MARKDOWN_BASE_EMOJI_URL
if MARTOR_MARKDOWNIFY_TIMEOUT:
attributes_to_pass["data-save-timeout"] = MARTOR_MARKDOWNIFY_TIMEOUT
# Make sure that the martor value is in the class attr passed in
if "class" in attrs:
attrs["class"] += " martor"
else:
attrs["class"] = "martor"
# Update and overwrite with the attributes passed in
attributes_to_pass.update(attrs)
# Update and overwrite with any attributes that are on the widget
# itself. This is also the only way we can push something in without
# being part of the render chain.
attributes_to_pass.update(self.attrs)
template = get_template("martor/%s/editor.html" % get_theme())
emoji_enabled = MARTOR_ENABLE_CONFIGS.get("emoji") == "true"
mentions_enabled = MARTOR_ENABLE_CONFIGS.get("mention") == "true"
widget = super().render(name, value, attributes_to_pass)
return template.render(
{
"martor": widget,
"field_name": name + "-" + random_string,
"emoji_enabled": emoji_enabled,
"mentions_enabled": mentions_enabled,
"toolbar_buttons": MARTOR_TOOLBAR_BUTTONS,
}
)
class Media:
selected_theme = get_theme()
css = {
"all": (
"plugins/css/ace.min.css",
"plugins/css/resizable.min.css",
"martor/css/martor.%s.min.css" % selected_theme,
)
}
if MARTOR_ENABLE_ADMIN_CSS:
admin_theme = ("martor/css/martor-admin.min.css",)
css["all"] = admin_theme.__add__(css.get("all"))
js = (
"plugins/js/ace.js",
"plugins/js/mode-markdown.js",
"plugins/js/ext-language_tools.js",
"plugins/js/theme-github.js",
"plugins/js/highlight.min.js",
"plugins/js/resizable.min.js",
"plugins/js/emojis.min.js",
"martor/js/martor.%s.min.js" % selected_theme,
)
# Adding the following scripts to the end
# of the tuple in case it affects behaviour.
# spellcheck configuration
if MARTOR_ENABLE_CONFIGS.get("spellcheck") == "true":
js = ("plugins/js/typo.js", "plugins/js/spellcheck.js").__add__(js)
# support alternative vendor theme file like: bootstrap, semantic)
# 1. vendor css theme
if MARTOR_ALTERNATIVE_CSS_FILE_THEME:
css_theme = MARTOR_ALTERNATIVE_CSS_FILE_THEME
css["all"] = (css_theme,).__add__(css.get("all"))
else:
css_theme = "plugins/css/%s.min.css" % selected_theme
css["all"] = (css_theme,).__add__(css.get("all"))
# 2. vendor js theme
if MARTOR_ALTERNATIVE_JS_FILE_THEME:
js_theme = MARTOR_ALTERNATIVE_JS_FILE_THEME
js = (MARTOR_ALTERNATIVE_JS_FILE_THEME,).__add__(js)
else:
js_theme = "plugins/js/%s.min.js" % selected_theme
js = (js_theme,).__add__(js)
# 3. vendor jQUery
if MARTOR_ALTERNATIVE_JQUERY_JS_FILE:
js = (MARTOR_ALTERNATIVE_JQUERY_JS_FILE,).__add__(js)
elif MARTOR_ENABLE_CONFIGS.get("jquery") == "true":
js = ("plugins/js/jquery.min.js",).__add__(js)
class AdminMartorWidget(MartorWidget, widgets.AdminTextareaWidget):
pass