n5321 | 2024年12月7日 11:01

Tags:


问题:

  1. 工具栏里面的style button有normal、quote and code!强需求是code要高亮出来,同行的做法是添加背景色,然后换行的距离缩短等等。应用default config的话,这几个功能好像没有什么效果!
    为了解决这个问题!一开始的思路是在form.py中找思路!

SUMMERNOTE_CONFIG = {
  'toolbar': [
      ['style', ['bold', 'italic', 'underline', 'clear']], # 样式按钮
      ['para', ['ul', 'ol', 'paragraph']], # 段落格式
      ['insert', ['link', 'picture', 'video']], # 插入内容
      ['view', ['fullscreen', 'codeview']], # 查看
      ['style', ['normal', 'blockquote', 'pre']], # 样式按钮,添加 normal, blockquote 和 code
  ],
  'styleTags': [
      'p', # 正常段落
      'blockquote', # 引用块
      'pre', # 代码块
      'h1', 'h2', 'h3', 'h4', 'h5', 'h6', # 标题
      'strong', 'em', 'u', # 强调、斜体、下划线
  ],
  'fontSizes': ['12', '14', '16', '18', '20', '24', '28', '32', '36'], # 字体大小
  'fontNames': ['Arial', 'Comic Sans MS', 'Courier New'], # 可用字体
  'height': 400, # 编辑器高度
}

通过这个代码 'strong', 'em', 'u', # 强调、斜体、下划线,这些内容都加进来了,但是normal,quote,code还是没有变化。

改思路:在 CSS 中自定义这些标签的样式

新建一个customSummernote.css的样式文档,文档放在static文件夹下,通过

<link rel="stylesheet" href="{% static 'css/customSummernote.css' %}">

实现链接

p {
   font-family: Arial, sans-serif;
   font-size: 14px;
   line-height: 1.6;
   color: #333;
   margin-bottom: 15px;
}

blockquote {
  font-style: italic;
   border-left: 5px solid #e74c3c; /* 警醒且稳重的红色 */
   padding-left: 15px;
   margin: 10px 0;
   background-color: #f4f6f9;
   color: #555;
   box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
}

pre {
   font-family: "Courier New", monospace;
   background-color: #b0b0b0; /* 更浅的灰色 */
   color: #333333;
   padding: 15px;
   border-radius: 5px;
   white-space: pre-wrap;
   word-wrap: break-word;
   box-shadow: 0 4px 6px rgba(128, 128, 128, 0.1); /* 使用灰色阴影 */
}

最后在渲染以后获得很好的效果。

可是是编辑环境下仍然是老样子!

考虑是否要换个编辑器,查了下django framework下的编辑器的市场行情

编辑器易用性功能丰富性轻量化推荐场景
Summernote⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐快速实现富文本功能
CKEditor⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐高级功能需求
TinyMCE⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐简单且兼容性好
Quill⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐现代、轻量富文本需求
MarkdownX⭐⭐⭐⭐⭐⭐⭐⭐⭐专注 Markdown,适合博客或技术文档
Trumbowyg⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐性能优先、适合简洁的文本编辑器需求

这样子起来summernote还是不错的。相对正式的来研究这个summernote编辑器!

summernote

本质上就是css&js的library,问题是调用的方式。通用的版本到了0.9,可是django-summernote只到0.81.看官网下的https://summernote.org/,在styletag上看起来很漂亮。当然问题是quote那个地方容易出错。

help里面说可以直接在settings.py中进行配置。于是开始尝试:

SUMMERNOTE_CONFIG = {
  'iframe': False,
  'stylesheets': [
      '/static/css/customSummernote.css',
  ],
  'summernote': {
      'airMode': False,
      'width': '100%',
      'height': '600',
      'lang': 'en-US',
      'toolbar': [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['fontname', ['fontname']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']],
          ['insert', ['link', 'picture', 'video']],
          ['view', ['fullscreen', 'codeview', 'help']],
      ],
      'codemirror': {
          'mode': 'htmlmixed',
          'lineNumbers': 'true',
          'theme': 'monokai',
      },
      'callbacks': {
          'onChange': 'function(contents, $editable) { console.log(contents); }',
      },
  },
  'attachment_require_authentication': True,
  'disable_attachment': False,
  'lazy': True,
}

用这个配置,好像也没有解决问题!本质上只是把配置文档从form.py挪到了settings.py之中而已。

考虑另外的办法。看它官方的处理方式!官方的example这个操作就集齐简单了。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Summernote</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/summernote@0.9.0/dist/summernote.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.9.0/dist/summernote.min.js"></script>
</head>
<body>
<div id="summernote"><p>Hello Summernote</p></div>
<script>
  $(document).ready(function() {
      $('#summernote').summernote();
  });
</script>
</body>
</html>

我现在的问题是要改变编辑环境下的渲染!

尝试在edit页面中直接用这个example的方式,效果不错,考虑refactoring code!自己的css&js引用有点又多又乱的意思 ,效果还是差了一点!

也许要把css&js做一个重构!

搞不定!,放弃了

关键的问题在于搞不定iframe,如何删掉这个iframe,help里面说的很简单。但是操作起来命名搞不成器!

如果从github上的update来看,summernote可能也不是一个完全成熟的产品。刚刚发现的新问题是在settings.py之中做设置的话,在admin里面又看不到了!

算了不管他了。