-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
964afc0
commit 2a5eace
Showing
67 changed files
with
1,586 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/basic-modules/src/modules/blockquote/parse-elem-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @description parse html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Descendant } from 'slate' | ||
import { Dom7Array } from 'dom7' | ||
import { IDomEditor } from '@wangeditor/core' | ||
import { BlockQuoteElement } from './custom-types' | ||
|
||
function parseHtml( | ||
$elem: Dom7Array, | ||
children: Descendant[], | ||
editor: IDomEditor | ||
): BlockQuoteElement { | ||
// 无 children ,则用纯文本 | ||
if (children.length === 0) { | ||
children = [{ text: $elem.text().replace(/\s+/gm, ' ') }] | ||
} | ||
|
||
return { | ||
type: 'blockquote', | ||
// @ts-ignore | ||
children, | ||
} | ||
} | ||
|
||
export const parseHtmlConf = { | ||
selector: 'blockquote', | ||
parseElemHtml: parseHtml, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/basic-modules/src/modules/code-block/parse-elem-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @description parse html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Descendant } from 'slate' | ||
import { Dom7Array } from 'dom7' | ||
import { IDomEditor, DomEditor } from '@wangeditor/core' | ||
import { PreElement, CodeElement } from './custom-types' | ||
|
||
function parseCodeHtml($elem: Dom7Array, children: Descendant[], editor: IDomEditor): CodeElement { | ||
return { | ||
type: 'code', | ||
language: '', // language 在 code-highlight 中实现 | ||
children: [{ text: $elem[0].textContent || '' }], | ||
} | ||
} | ||
|
||
export const parseCodeHtmlConf = { | ||
selector: 'code', | ||
parseElemHtml: parseCodeHtml, | ||
} | ||
|
||
function parsePreHtml($elem: Dom7Array, children: Descendant[], editor: IDomEditor): PreElement { | ||
return { | ||
type: 'pre', | ||
// @ts-ignore | ||
children: children.filter(child => DomEditor.getNodeType(child) === 'code'), | ||
} | ||
} | ||
|
||
export const parsePreHtmlConf = { | ||
selector: 'pre', | ||
parseElemHtml: parsePreHtml, | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/basic-modules/src/modules/code-block/pre-parse-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @description pre parse html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Dom7Array } from 'dom7' | ||
import { getTagName } from '../../utils/dom' | ||
|
||
/** | ||
* pre-prase <code> ,去掉其中的 <xmp> (兼容 V4) | ||
* @param $code $code | ||
*/ | ||
function preParse($code: Dom7Array) { | ||
const tagName = getTagName($code) | ||
if (tagName !== 'code') return $code | ||
|
||
const $xmp = $code.find('xmp') | ||
if ($xmp.length === 0) return $code // 不是 V4 格式 | ||
|
||
const codeText = $xmp.text() | ||
$xmp.remove() | ||
$code.text(codeText) | ||
|
||
return $code | ||
} | ||
|
||
export const preParseHtmlConf = { | ||
selector: 'pre>code', // 匹配 <pre> 下的 <code> | ||
preParseHtml: preParse, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/basic-modules/src/modules/color/parse-style-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @description parse style html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Dom7Array } from 'dom7' | ||
import { Descendant, Text } from 'slate' | ||
import { ColorText } from './custom-types' | ||
import { getStyleValue } from '../../utils/dom' | ||
|
||
export function parseStyleHtml($text: Dom7Array, node: Descendant): Descendant { | ||
if (!Text.isText(node)) return node | ||
|
||
const textNode = node as ColorText | ||
|
||
const color = getStyleValue($text, 'color') | ||
if (color) { | ||
textNode.color = color | ||
} | ||
|
||
const bgColor = getStyleValue($text, 'background-color') | ||
if (bgColor) { | ||
textNode.bgColor = bgColor | ||
} | ||
|
||
return textNode | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/basic-modules/src/modules/color/pre-parse-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @description pre-parse html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Dom7Array } from 'dom7' | ||
import { getTagName } from '../../utils/dom' | ||
|
||
/** | ||
* pre-prase font ,兼容 V4 | ||
* @param $font $font | ||
*/ | ||
function preParse($font: Dom7Array) { | ||
const tagName = getTagName($font) | ||
if (tagName !== 'font') return $font | ||
|
||
// 处理 color (V4 使用 <font color="#ccc">xx</font> 格式) | ||
const color = $font.attr('color') || '' | ||
if (color) { | ||
$font.removeAttr('color') | ||
$font.css('color', color) | ||
} | ||
|
||
return $font | ||
} | ||
|
||
export const preParseHtmlConf = { | ||
selector: 'font', | ||
preParseHtml: preParse, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/basic-modules/src/modules/divider/parse-elem-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @description parse html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Descendant } from 'slate' | ||
import { Dom7Array } from 'dom7' | ||
import { IDomEditor } from '@wangeditor/core' | ||
import { DividerElement } from './custom-types' | ||
|
||
function parseHtml($elem: Dom7Array, children: Descendant[], editor: IDomEditor): DividerElement { | ||
return { | ||
type: 'divider', | ||
children: [{ text: '' }], // void node 有一个空白 text | ||
} | ||
} | ||
|
||
export const parseHtmlConf = { | ||
selector: 'hr', | ||
parseElemHtml: parseHtml, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/basic-modules/src/modules/font-size-family/parse-style-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @description parse style html | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Dom7Array } from 'dom7' | ||
import { Descendant, Text } from 'slate' | ||
import { FontSizeAndFamilyText } from './custom-types' | ||
import { getStyleValue } from '../../utils/dom' | ||
|
||
export function parseStyleHtml($text: Dom7Array, node: Descendant): Descendant { | ||
if (!Text.isText(node)) return node | ||
|
||
const textNode = node as FontSizeAndFamilyText | ||
|
||
const fontSize = getStyleValue($text, 'font-size') | ||
if (fontSize) { | ||
textNode.fontSize = fontSize | ||
} | ||
|
||
const fontFamily = getStyleValue($text, 'font-family') | ||
if (fontFamily) { | ||
textNode.fontFamily = fontFamily | ||
} | ||
|
||
return textNode | ||
} |
Oops, something went wrong.