Skip to content

Commit

Permalink
fix: getHtml API
Browse files Browse the repository at this point in the history
  • Loading branch information
wangfupeng1988 committed Aug 12, 2021
1 parent 5955b61 commit c0b60cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IDomEditor } from '@wangeditor/core'

function pToHtml(elem: Element, childrenHtml: string, editor: IDomEditor): string {
if (childrenHtml === '') {
return '<p><br></p>'
return '<p><br/></p>'
}
return `<p>${childrenHtml}</p>`
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/editor/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface IDomEditor extends Editor {

// 内容处理
handleTab: () => void
getHtml: (withFormat?: boolean) => string
getHtml: (opt?: { withFormat?: boolean; containerClassName?: string }) => string
getText: () => string
getSelectionText: () => string // 获取选区文字
getHeaders: () => { id: string; type: string; text: string }[] // 获取所有标题
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/editor/plugins/with-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { node2html } from '../../to-html/node2html'
import { genElemId } from '../../formats/helper'
import { Key } from '../../utils/key'
import { findCurrentLineRange } from '../../utils/line'
import $ from '../../utils/dom'

export const withContent = <T extends Editor>(editor: T) => {
const e = editor as T & IDomEditor
Expand Down Expand Up @@ -126,16 +125,21 @@ export const withContent = <T extends Editor>(editor: T) => {
}

// 获取 html
e.getHtml = (withFormat = true): string => {
// TODO 参数补充到文档中
e.getHtml = (opt: { withFormat?: boolean; containerClassName?: string } = {}): string => {
const { withFormat = true, containerClassName = 'w-e-content-container' } = opt

const { children = [] } = e
let html = children.map(child => node2html(child, e)).join('')
html = `<div>${html}</div>`
html = `<div class="${containerClassName}">${html}</div>`

if (withFormat) {
return xmlFormat(html, {
// 格式化
html = xmlFormat(html, {
collapseContent: true,
})
}

return html
}

Expand Down

0 comments on commit c0b60cf

Please sign in to comment.