Giter Club home page Giter Club logo

braft-convert's Introduction

braft-convert

与braft-editor搭配使用的数据转换工具

使用方法
# use yarn
yarn add braft-convert
# use npm
npm install --save braft-convert
import { convertRawToHTML, convertHTMLToRaw } from 'braft-convert'

const draftRawData = '{"blocks":[{"key":"9hu83","text":"Hello World!","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":6,"length":5,"style":"BOLD"},{"offset":6,"length":5,"style":"COLOR-F32784"}],"entityRanges":[],"data":{}}],"entityMap":{}}'
const htmlString = '<p>Hello <strong><span style="color:#f32784">World</span></strong>!</p>'

convertRawToHTML(JSON.parse(draftRawData))
// <p>Hello <strong><span style="color:#f32784">World</span></strong>!</p>

JSON.stringify(convertHTMLToRaw(htmlString))
// {"blocks":[{"key":"8v6eh","text":"Hello World!","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":6,"length":5,"style":"BOLD"},{"offset":6,"length":5,"style":"COLOR-F32784"}],"entityRanges":[],"data":{}}],"entityMap":{}}

braft-convert's People

Contributors

jsososo avatar margox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

braft-convert's Issues

Custom block types from blockRenderMap do not translate into `toHTML` conversion.

First off, THANK YOU. This project is amazing and I really like everything you've built, @margox.

I have a small problem.
I have been playing with the editor and trying to render some custom blocks. For example:
I am using ContentUtils.toggleSelectionBlockType and inserting custom block <column-1>.
on the Braft Preview it works great!

However, when saving this HTML to the database, converting custom elements is trickier. I get
convertToHTML: missing HTML definition for block with type "column-1". I want to save HTML String to database ( I know i should use RAW, but right now I need HTML string).

I looked at HubSpot's DraftConvert, and there is options to pass to do something like:

    const convertedHTML = editorState.converToHTML({
      blockToHTML: block => {
        if (block.type.split('-')[0] === 'column') {
          return <div style={{ columnCount: block.type.split('-')[1] }} />;
        }
      },
    })(editorState.getCurrentContent());

however, when i tried to use it, all custom braft styling is now missing! What is your recommendation to convert custom HTML blocks?

toHTML() used with converts={{blockExportFn, blockImportFn}}

Hi,
When I use blockExportFn to export unstyled blocks as divs, the function toHTML() is ignoring styling of alignment and indent. I see that there is specific implementation for these cases in blockToHTML (draft-convert). But for some reason it doesnt happen. Would be happy to understand better (There are no much docs except for https://braft.margox.cn/demos/block page)

Please try to align (center/right) or indent text in the following Sandbox reproduction:
https://codesandbox.io/s/braft-convert-trial-365mw

Thanks

braft-convert 转换 粗体/超链 会导致空格丢失

Reproduction link

Edit on CodeSandbox

Steps to reproduce

详情请见重现链接。
复制一段内容下来,将原版和转换后再转回来的内容展示在网页中。
发现braft-convert转换的空格丢失。

convertRawToHTML(convertHTMLToRaw(htmlString));

image

What is expected?

期望转换后的内容和原版一致。

What is actually happening?

转换后空格丢失

Environment Info
braft-convert 2.3.0
React 16.8

Backwards Arguments for convertEditorStateToHTML

Ran into a small issue because there isn't a lot of documentation with blockExportFn and styleExportFn:

These two export Fn's expect editorState as 1st or 2nd argument! It is not uniform!

blockExportFn: (editorState, block) => { }; 
styleExportFn: (style, editorState) => { }; 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.