搜索插件、代码仓与 APP 分类

插件详情v1.0.5

vsc_quill_delta_to_html

一个强大的 Quill Delta 到 HTML 转换器。这是流行的 quill-delta-to-html Typescript/Javascript 包的完整 Dart 版本移植。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
12.4万
喜欢数
50
Pub 分数
140

安装插件

flutter pub add vsc_quill_delta_to_html

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个强大的 Dart 实现的 quill-delta-to-html 转换器,可实现 Quill Delta 格式到干净 HTML 的无缝转换。该实现完全移植自流行的 TypeScript/JavaScript 库,确保在 Flutter 平台上一致地渲染富文本内容。适用于需要精确控制格式化文本输出的应用程序,例如编辑器、内容管理系统或发布工具。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 将 Quill 编辑器内容转换为 HTML
  • 在网页视图中渲染富文本
  • 与CMS或发布工作流程集成
  • 从应用程序中的用户输入生成 HTML
  • 支持跨平台富文本显示

主要特点

  • quill-delta-to-html 的完整移植
  • 支持所有 Quill Delta 操作
  • 跨平台兼容性
  • 干净且语义化的 HTML 输出
  • 轻松集成到Flutter应用

适合对象

  • 使用 Quill 编辑器的 Flutter 应用
  • 内容分发系统
  • 富文本渲染需要
  • Web和移动混合应用
  • 从 JavaScript Quill 迁移的开发者

使用建议

  • 需要了解 Quill Delta 格式
  • 可能需要为HTML输出添加额外的样式
  • 不适合实时协作
  • 仅限转换,不可编辑
  • 依赖外部CSS进行呈现

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

VSC Quill Delta to HTML Converter

Converts Quill's Delta format to HTML (insert ops only) with properly nested lists. It has full support for Quill operations - including images, videos, formulas, tables, and mentions. Conversion can be performed in vanilla Dart (i.e., server-side or CLI) or in Flutter.

This is a complete port of the popular quill-delta-to-html Typescript/Javascript package to Dart.

This converter can convert to HTML for a number of purposes, not the least of which is for generating HTML-based emails. It makes a great pairing with Flutter Quill.

You can try a live demo of the conversion by running the example Flutter app.

Quickstart

To get started, add this package to your pubspec.yaml dependencies.

Usage

import 'package:vsc_quill_delta_to_html/vsc_quill_delta_to_html.dart';

void main() {
  final deltaOps = [
    {'insert': 'Hello\n'},
    {
      'insert': 'This is colorful',
      'attributes': {'color': '#f00'}
    }
  ];

  final converter = QuillDeltaToHtmlConverter(
    ops,
    ConverterOptions.forEmail(),
  );

  final html = converter.convert();
  print(html);
}

Configuration

QuillDeltaToHtmlConverter accepts a few configuration (ConverterOptions, OpConverterOptions, and OpAttributeSanitizerOptions) options as shown below:

Option Type Default Description
converterOptions.paragraphTag string 'p' Custom tag to wrap inline html elements
converterOptions.encodeHtml boolean true If true, <, >, /, ', ", & characters in content will be encoded.
converterOptions.classPrefix string 'ql' A css class name to prefix class generating styles such as size, font, etc.
converterOptions.inlineStylesFlag boolean false If true, use inline styles instead of classes.
converterOptions.inlineStyles InlineStyles null If non-null, use inline styles instead of classes. See Rendering Inline Styles section below for usage.
multiLineBlockquote boolean true Instead of rendering multiple blockquote elements for quotes that are consecutive and have same styles(align, indent, and direction), it renders them into only one
multiLineHeader boolean true Same deal as multiLineBlockquote for headers
multiLineCodeblock boolean true Same deal as multiLineBlockquote for code-blocks
multiLineParagraph boolean true Set to false to generate a new paragraph tag after each enter press (new line)
multiLineCustomBlock boolean true Same deal as multiLineBlockquote for custom blocks.
bulletListTag string 'ul' Tag for unordered bullet lists.
orderedListTag string 'ol' Tag for ordered/numbered lists.
converterOptions.linkRel string none generated Specifies a value to put on the rel attr on all links. This can be overridden by an individual link op by specifying the rel attribute in the respective op's attributes
converterOptions.linkTarget string '_blank' Specifies target for all links; use '' (empty string) to not generate target attribute. This can be overridden by an individual link op by specifiying the target with a value in the respective op's attributes.
converterOptions.allowBackgroundClasses boolean false If true, css classes will be added for background attr
sanitizerOptions.urlSanitizer String? Function(String url) null A function that is called once per url in the ops (image, video, link) for you to do custom sanitization. If your function returns a string, it is assumed that you sanitized the url and no further sanitization will be done by the library; when anything other than a string is returned (e.g. undefined), it is assumed that no sanitization has been done and the library's own function will be used to clean up the url
sanitizerOptions.allow8DigitHexColors boolean false If true, hex colors in #AARRGGBB format are allowed in the ops
converterOptions.customTag String? Function(String format, DeltaInsertOp op) null Callback allows to provide custom html tag for some format
converterOptions.customTagAttributes Map<String, String>? Function(DeltaInsertOp op) null Allows custom html tag attributes for the given op
converterOptions.customCssClasses List<String>? Function(DeltaInsertOp op) null Allows custom CSS classes for the given op
converterOptions.customCssStyles List<String>? Function(DeltaInsertOp op) null Allows custom CSS styles attributes for the given op

Rendering Quill Formats

You can customize the rendering of Quill formats by registering render callbacks before calling the convert() method.

There is a beforeRender and an afterRender callback. They are called multiple times before and after rendering each group. A group is one of:

  • continuous sets of inline elements
  • a video element
  • list elements
  • block elements (header, code-block, blockquote, align, indent, and direction)

beforeRender event is called with raw operation objects for you to generate and return your own html. If you return null, the system will return its own generated html.

afterRender event is called with generated html for you to inspect, maybe make some changes, and return your modified or original html.

converter.beforeRender = (GroupType groupType, TDataGroup data) {
    // ... generate your own html 
    // return your html
};

converter.afterRender = (GroupType groupType, String htmlString) {
    // modify if you wish
    // return the html
};

html = converter.convert();

Following shows the parameter formats for beforeRender event:

groupType data
video VideoItem
block BlockGroup
list ListGroup
inline-group InlineGroup

Rendering Inline Styles

If you are rendering to HTML that you intend to include in an email, using classes and a style sheet are not recommended, as not all browsers support style sheets. vsc_quill_delta_to_html supports rendering inline styles instead. The easiest way to enable this is to pass the option inlineStylesFlag: true.

You can customize styles by passing an object to inlineStyles instead:

inlineStyles: InlineStyles({
  'font': InlineStyleType(
      fn: (value, _) => defaultInlineFonts[value] ?? 'font-family:$value'),
  'size': InlineStyleType(map: {
    'small': 'font-size: 0.75em',
    'large': 'font-size: 1.5em',
    'huge': 'font-size: 2.5em',
  }),
  'indent': InlineStyleType(fn: (value, op) {
    final indentSize = (double.tryParse(value) ?? double.nan) * 3;
    final side = op.attributes['direction'] == 'rtl' ? 'right' : 'left';
    return 'padding-$side:${indentSize}em';
  }),
  'direction': InlineStyleType(fn: (value, op) {
    if (value == 'rtl') {
      final textAlign =
      isTruthy(op.attributes['align']) ? '' : '; text-align:inherit';
      return ('direction:rtl$textAlign');
    } else {
      return null;
    }
  }),
  'list': InlineStyleType(map: {
    'checked': "list-style-type:'\\2611';padding-left: 0.5em;",
    'unchecked': "list-style-type:'\\2610';padding-left: 0.5em;",
  }),
});

Keys to this object are the names of attributes from Quill. The values are either a simple lookup table (like in the list example above) used to map values to styles, or a fn(value, op) which returns a style string.

Rendering Custom Blot Formats

You need to tell system how to render your custom blot by registering a renderer callback function to renderCustomWith method before calling the convert() method.

If you would like your custom blot to be rendered as a block (not inside another block or grouped as part of inlines), then add renderAsBlock: true to its attributes.

Example:

final ops = [
    {insert: {'my-blot': {id: 2, text: 'xyz'}}, attributes: {renderAsBlock: true|false}}
];

let converter = new QuillDeltaToHtmlConverter(ops);

// customOp is your custom blot op
// contextOp is the block op that wraps this op, if any. 
// If, for example, your custom blot is located inside a list item,
// then contextOp would provide that op. 
converter.renderCustomWith = (DeltaInsertOp customOp, DeltaInsertOp? contextOp) {
    if (customOp.insert.type == 'my-blot') {
        return '<span>Some custom blot</span>';
    } else {
        return 'Unmanaged custom blot!';
    }
});

html = converter.convert();

customOp object will have the following format:

{
    insert: {
        type: string //whatever you specified as key for insert, in above example: 'my-blot'
        value: any // value for the custom blot  
    }, 
    attributes: {
        // ... any attributes custom blot may have
    }
}

Advanced Custom Rendering Using Grouped Ops

If you want to do the full rendering yourself, you can do so by getting the processed & grouped ops.

final groupedOps = converter.getGroupedOps();

Each element in groupedOps array will be an instance of the following types: InlineGroup, VideoItem, BlockGroup, ListGroup, ListItem, BlotBlock.

BlotBlock represents custom blots with renderAsBlock:true property pair in its attributes

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目