Skip to content

Commit

Permalink
add dynamic id to MD Editor widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
kumkao committed Jun 24, 2023
1 parent e78562c commit 4f99f68
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
52 changes: 52 additions & 0 deletions mdeditor/templates/markdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,58 @@
//this.resize("100%", 640);
}
});
// workaround for admin inline
editormd("{{ id }}-wmd-wrapper", {
watch: {{ config.watch|lower }}, // 关闭实时预览
lineNumbers: {{ config.lineNumbers|lower }},
lineWrapping: {{ config.lineWrapping|lower }},
width: "{{ config.width }}",
height: {{ config.height }},
placeholder: '{{ config.placeholder }}',
// 当有多个mdeditor时全屏后其他mdeditor仍然显示解决此问题
onfullscreen : function() {
this.editor.css("border-radius", 0).css("z-index", 9999);
},
onfullscreenExit : function() {
this.editor.css({
zIndex : 10,
border : "1px solid rgb(221,221,221)"
})
},
syncScrolling: "single",
path: "{% static 'mdeditor/js/lib/' %}",
// theme
theme : "{{ config.theme|safe }}",
previewTheme : "{{ config.preview_theme|safe }}",
editorTheme : "{{ config.editor_theme }}",

saveHTMLToTextarea: true, // editor.md 有问题没有测试成功
toolbarAutoFixed: {{ config.toolbar_autofixed|lower }},
searchReplace: {{ config.search_replace|lower }},
emoji: {{ config.emoji|lower }},
tex: {{ config.tex|lower }},
taskList: {{ config.task_list|lower }},
flowChart: {{ config.flow_chart|lower }},
sequenceDiagram: {{ config.sequence|lower }},

// image upload
imageUpload: true,
imageFormats: {{ config.upload_image_formats|safe }},
imageUploadURL: "{{ config.upload_image_url }}",
toolbarIcons: function () {
return {{ config.toolbar|safe }}
},
onload: function () {
console.log('onload', this);
//this.fullscreen();
//this.unwatch();
//this.watch().fullscreen();

//this.setMarkdown("#PHP");
//this.width("100%");
//this.height(480);
//this.resize("100%", 640);
}
});
});
</script>
10 changes: 9 additions & 1 deletion mdeditor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.utils.encoding import force_text
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
import random
import string

try:
# Django >=1.7
Expand All @@ -17,6 +19,12 @@
from .configs import MDConfig


def generate_random_text(length):
characters = string.ascii_letters + string.digits
random_text = ''.join(random.choice(characters) for _ in range(length))
return random_text


class MDEditorWidget(forms.Textarea):
"""
Widget providing Editor.md for Rich Text Editing.
Expand All @@ -38,7 +46,7 @@ def render(self, name, value, renderer=None, attrs=None):
return mark_safe(render_to_string('markdown.html', {
'final_attrs': flatatt(final_attrs),
'value': conditional_escape(force_text(value)),
'id': final_attrs['id'],
'id': generate_random_text(10) + '_' + final_attrs['id'],
'config': self.config,
}))

Expand Down

0 comments on commit 4f99f68

Please sign in to comment.