Skip to content
网游世界
网游世界

吾生有涯,而知无涯。

  • 首页
  • PHP
    • ThinkPHP
    • FastAdmin
    • webman
  • JavaScript
    • jQuery
    • AdminLTE
  • Free Pascal
  • Java
    • JeeSite
    • 若依
    • ruoyi-vue-pro
  • 其它
    • 操作系统
    • 树莓派
    • 前端
    • Null
  • 关于
网游世界

吾生有涯,而知无涯。

FastAdmin Summernote 清除格式

3Vshej, 2024年2月3日 周六2024年2月26日 周一

需求是这样的,录入内容时,大多是从 Word 中粘贴过来的,内容很少,但实际代码却很多,存储时,提示保存失败。
最简单的方法是调整数据库字段长度,但这样不是最好的解决方法。

留意,必须在,/addons/summernote/bootstrap.js 文件中,增加内容,否则在 FastAdmin 后台清除插件缓存后,/public/assets/js/addons.js 会被覆盖!

另一种方式是通过对 Summernote 修改,实现粘贴即去除格式,或者,通过点击按钮去除格式。(插件自带的清除格式按钮,效果不是很好)。

方法一、粘贴后自动清除格式

在 /addons/summernote/bootstrap.js 文件中,增加函数:

// 清理粘贴的内容
function CleanPastedHTML(input) {
    let i;
    // 1. 移除换行符 和 Mso 类
    const stringStripper = /(\n|\r| class=(")?Mso[a-zA-Z]+(")?)/g;
    let output = input.replace(stringStripper, ' ');
    // 2. Word 脚本生成的 HTML 注释
    const commentStripper = new RegExp('<!--(.*?)-->', 'g');
    output = output.replace(commentStripper, '');
    let tagStripper = new RegExp('<(/)*(meta|link|span|\\?xml:|st1:|o:|font|b|i|strong|u|strike)(.*?)>', 'gi');
    // 3. 如果有内容则删除标签
    output = output.replace(tagStripper, '');
    // 4. 移除所有内容,像 '<style(.)style(.)>'
    const badTags = ['style', 'script', 'applet', 'embed', 'noframes', 'noscript'];

    for (i = 0; i< badTags.length; i++) {
        tagStripper = new RegExp('<'+badTags[i]+'.*?'+badTags[i]+'(.*?)>', 'gi');
        output = output.replace(tagStripper, '');
    }
    // 5. 移除属性,如 ' style="..."'
    const badAttributes = ['style', 'start'];
    for (i = 0; i< badAttributes.length; i++) {
        const attributeStripper = new RegExp(' ' + badAttributes[i] + '="(.*?)"', 'gi');
        output = output.replace(attributeStripper, '');
    }
    return output;
}

及在这个文件中,搜索 callback ,并增加以下:

(可以不使用 bufferText = CleanPastedHTML(bufferText);,只做简单清理)

onPaste: function (ne) {
    let bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
    bufferText = CleanPastedHTML(bufferText);
    ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
    setTimeout(function () {
        document.execCommand("insertText", false, bufferText);
    }, 10);
},

方法二:手动清除格式

即,增加按钮,点击后清除格式。仍是在 /addons/summernote/bootstrap.js 文件中,增加函数:

// 自定义 清除格式按钮
const CleanWordButton = function (context) {
    const ui = $.summernote.ui;
    const button = ui.button({
        contents: '<i class="note-icon-eraser"/>',
        tooltip: '清除格式',
        click: function () {
            //  清除img的wrap元素
            let str = context.invoke('code');
            str = CleanPastedHTML(str);
            context.invoke('code', str);
        }
    });
    return button.render();
};

及搜索 toolbar 并增加 word 项,及在 buttons 中定义 word 按钮要执行方法:

toolbar: [
    ['view', ['fullscreen', 'codeview', 'word', 'help']],
],
buttons: {
    image: imageButton,
    attachment: attachmentButton,
    word: CleanWordButton
},

至此完成。

参考资料:

  • Onpaste event to clear formatting
  • summernote 去除复制文本样式
  • 基于summernote的富文本编辑器,粘贴时去除word格式

相关文章:

  1. FastAdmin 添加、编辑页复用 FastAdmin 中,大多情况下,添加页与编辑页内容是相同的,区别在于添加页无默认数据,编辑页需要......
  2. Paginationjs 插件简单使用 在日常工作中,分页很常用,但有时候,不想让后端处理,改为前端 JS 实现分页效果。 前端分页通常适用......
  3. Paginationjs 插件 Ajax 请求分页数据 上一文说到过 Paginationjs 插件简单使用,它只是针对于少量数据。 Paginationj......
  4. jQuery 创建表单并提交 在开发中,需要单独提交一个表单,如文件下载:已知外部参数和提交地址,后端需要根据参数,整理数据,创建......
FastAdmin JavaScript jQuery PHP FastAdmin 框架Summernote 插件

文章导航

Previous post
Next post

近期文章

  • Android Studio Gradle 配置国内镜像
  • 为什么重新发明轮子
  • ruoyi-vue-pro 匿名访问
  • VUE 中接收 code 异常
  • 关于 AI

归档

  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
除非特殊说明,本站作品采用知识共享署名 4.0 国际许可协议进行许可。
豫公网安备 41010402002622号 豫ICP备2020029609号-3
©2025 3Vshej