diff --git a/packages/@typex-core/model/content.js b/packages/@typex-core/model/content.js index 59236eb..707e3b1 100644 --- a/packages/@typex-core/model/content.js +++ b/packages/@typex-core/model/content.js @@ -439,14 +439,14 @@ export default class Content extends Component { /** * @description 格式设置 - * @param {*} range + * @param {Range} range * @param {function} callback 格式处理回调 * @memberof Content * @instance */ setFormat (range, callback) { const commonPath = range.startContainer.queryCommonPath(range.endContainer) - const selectedPath = this.$editor.selection.getSeletedPath() + const selectedPath = this.$editor.selection.getLeafPaths() for (const path of selectedPath) { callback(path) } @@ -455,4 +455,23 @@ export default class Content extends Component { this.$editor.selection.drawRangeBg() }) } + + /** + * @description 组件设置 + * @param {Range} range + * @param {function} callback 格式处理回调 + * @memberof Content + * @instance + */ + setComponent (range, callback) { + const commonPath = range.startContainer.queryCommonPath(range.endContainer) + const selectedPath = this.$editor.selection.getPeerPaths() + for (const path of selectedPath) { + callback(path) + } + commonPath.currentComponent.$path.parent.currentComponent.update().then(() => { + range.updateCaret() + this.$editor.selection.drawRangeBg() + }) + } } diff --git a/packages/@typex-core/selection/index.js b/packages/@typex-core/selection/index.js index 1e994fe..d4cb3de 100644 --- a/packages/@typex-core/selection/index.js +++ b/packages/@typex-core/selection/index.js @@ -378,7 +378,7 @@ export default class Selection { * @memberof Selection * @instance */ - getSeletedPath () { + getLeafPaths () { if (this.collapsed) return [] const range = this.ranges[0] let start, @@ -433,6 +433,17 @@ export default class Selection { }, } } + + /** + * @description 获取同级path + * @memberof Selection + */ + getPeerPaths () { + if (this.collapse) { + return [this.ranges[0].container.currentComponent.$path] + } + } + recoverRangesFromSnapshot (rangesSnapshot) { this.removeAllRanges() this.ranges = rangesSnapshot.map((jsonRange) => diff --git a/packages/@typex-platform/web/dom.js b/packages/@typex-platform/web/dom.js index cf5f842..cfe81ea 100644 --- a/packages/@typex-platform/web/dom.js +++ b/packages/@typex-platform/web/dom.js @@ -26,7 +26,6 @@ export function appendChild (parentNode, newNode) { } export function removeChild (parentNode, referenceNode) { execDestory(referenceNode) - console.log(1); return parentNode.removeChild(referenceNode) } diff --git a/packages/editor/data.js b/packages/editor/data.js index fc917b7..d8f05d8 100644 --- a/packages/editor/data.js +++ b/packages/editor/data.js @@ -21,7 +21,6 @@ export const mockData = { data: [ { data: '这是1121一个基于Typex编写的简单富文本编辑器demo', - formats: { color: '#666', bold: true, fontSize: '20px' }, }, ], formats: { paragraph: true }, diff --git a/packages/editor/formats/components/Header.js b/packages/editor/formats/components/Header.js index 8d41c88..38cac2b 100644 --- a/packages/editor/formats/components/Header.js +++ b/packages/editor/formats/components/Header.js @@ -8,11 +8,6 @@ import Block from './block' export default class Header extends Block { render (h) { - // return h(`h${this.props.level}`, null, this.$path.render()) - return ( -

- {this.$path.render()} -

- ) + return h(`h${this.props.level}`, null, this.$path.render()) } } diff --git a/packages/editor/toolBar/compinents/Dialog.js b/packages/editor/toolBar/compinents/Dialog.js index ad77fb4..97beffd 100644 --- a/packages/editor/toolBar/compinents/Dialog.js +++ b/packages/editor/toolBar/compinents/Dialog.js @@ -9,7 +9,7 @@ export class Dialog extends Component { return (
{this.state.visiable ? ( -
+
{this.props.children.length ? this.props.children : 'dialog'}
) : ( diff --git a/packages/editor/toolBar/compinents/DialogContent.js b/packages/editor/toolBar/compinents/DialogContent.js index c66a2d0..739fe45 100644 --- a/packages/editor/toolBar/compinents/DialogContent.js +++ b/packages/editor/toolBar/compinents/DialogContent.js @@ -23,7 +23,8 @@ const comMap = { header: (h, self) => { return
headerClickHandle(e, self)} style="color:#666"> { - headerOps.map(ele =>
{ele[1]}
) + // headerOps.map(ele =>
{ele[1]}
) + headerOps.map(ele => h(ele[1], { "data-headervalue": ele[0], style: { margin: '2px 0' }, class: 'header-selector-item' }, [ele[1]])) }
} diff --git a/packages/editor/toolBar/compinents/toolBar.styl b/packages/editor/toolBar/compinents/toolBar.styl index 201c711..bea1a01 100644 --- a/packages/editor/toolBar/compinents/toolBar.styl +++ b/packages/editor/toolBar/compinents/toolBar.styl @@ -3,7 +3,11 @@ border solid 1px #eee background rgb(40 40 40) padding 6px - + + .header-selector-item:hover{ + background: #ddd; + color: #2ac9f9 + } .icon width 1.2em height 1.2em diff --git a/packages/editor/toolBar/toolBarOptions.js b/packages/editor/toolBar/toolBarOptions.js index 01d9529..6aaf90b 100644 --- a/packages/editor/toolBar/toolBarOptions.js +++ b/packages/editor/toolBar/toolBarOptions.js @@ -21,11 +21,10 @@ const toolBarOptions = [ name: 'header', icon: '#icon-header', showDialog: true, - commandHandle: editor => { - editor.$path.children[1].node.formats.header = 1 - editor.$path.currentComponent.update().then(() => { - }) - } + commandHandle: (editor, level) => setComponent(editor, path => { + console.log(path); + path.node.formats = { header: level } + }) }, { tooltip: '字体大小', @@ -117,4 +116,9 @@ function setFormat (editor, callback) { editor.selection.ranges.forEach((range) => { editor.$path.currentComponent.setFormat(range, callback) }) +} +function setComponent (editor, callback) { + editor.selection.ranges.forEach((range) => { + editor.$path.currentComponent.setComponent(range, callback) + }) } \ No newline at end of file