gpt4 book ai didi

javascript - Draft-JS - 如何使用一些不可编辑的文本创建自定义 block

转载 作者:行者123 更新时间:2023-11-29 17:46:07 25 4
gpt4 key购买 nike

在 Draft-JS 中,我想要一个基本的自定义 block ,渲染一个 <h1>元素。我想在 h1 之前添加一些文字,用户无法编辑。此处的文字是为了告知人们此 block 用于 Title。所以我想在不可编辑的 block 前面加上“TITLE”。

在 Draft JS 中实现此目标的最佳方法是什么?

最佳答案

您可以通过在应该是只读的节点上应用 contentEditable={false}readOnly 属性来实现您的目标:

class MyCustomBlock extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<div className="my-custom-block">
<h1
contentEditable={false} // <== !!!
readOnly // <== !!!
>
Not editable title
</h1>
<div className="editable-area">
<EditorBlock {...this.props} />
</div>
</div>
);
}
}

在下面的隐藏片段中检查工作演示:

const {Editor, CharacterMetadata, DefaultDraftBlockRenderMap, ContentBlock, EditorBlock, genKey, ContentState, EditorState} = Draft;
const { List, Map, Repeat } = Immutable;

class MyCustomBlock extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<div className="my-custom-block">
<h1
contentEditable={false}
readOnly
>
Not editable title
</h1>
<div className="editable-area">
<EditorBlock {...this.props} />
</div>
</div>
);
}
}

function blockRendererFn(contentBlock) {
const type = contentBlock.getType();

if (type === 'MyCustomBlock') {
return {
component: MyCustomBlock,
props: {}
};
}
}

const RenderMap = new Map({
MyCustomBlock: {
element: 'div',
}
}).merge(DefaultDraftBlockRenderMap);

const extendedBlockRenderMap = Draft.DefaultDraftBlockRenderMap.merge(RenderMap);

class Container extends React.Component {
constructor(props) {
super(props);

this.state = {
editorState: EditorState.createEmpty()
};
}

_handleChange = (editorState) => {
this.setState({ editorState });
}

_onAddCustomBlock = () => {
const selection = this.state.editorState.getSelection();

this._handleChange(addNewBlockAt(
this.state.editorState,
selection.getAnchorKey(),
'MyCustomBlock'
))
}

render() {
return (
<div>
<div className="container-root">
<Editor
placeholder="Type"
blockRenderMap={extendedBlockRenderMap}
blockRendererFn={blockRendererFn}
editorState={this.state.editorState}
onChange={this._handleChange}
/>
</div>
<button onClick={this._onAddCustomBlock}>
ADD CUSTOM BLOCK
</button>
</div>
);
}
}

ReactDOM.render(<Container />, document.getElementById('react-root'));

const addNewBlockAt = (
editorState,
pivotBlockKey,
newBlockType = 'unstyled',
initialData = new Map({})
) => {
const content = editorState.getCurrentContent();
const blockMap = content.getBlockMap();
const block = blockMap.get(pivotBlockKey);

if (!block) {
throw new Error(`The pivot key - ${ pivotBlockKey } is not present in blockMap.`);
}

const blocksBefore = blockMap.toSeq().takeUntil((v) => (v === block));
const blocksAfter = blockMap.toSeq().skipUntil((v) => (v === block)).rest();
const newBlockKey = genKey();

const newBlock = new ContentBlock({
key: newBlockKey,
type: newBlockType,
text: '',
characterList: new List(),
depth: 0,
data: initialData,
});

const newBlockMap = blocksBefore.concat(
[[pivotBlockKey, block], [newBlockKey, newBlock]],
blocksAfter
).toOrderedMap();

const selection = editorState.getSelection();

const newContent = content.merge({
blockMap: newBlockMap,
selectionBefore: selection,
selectionAfter: selection.merge({
anchorKey: newBlockKey,
anchorOffset: 0,
focusKey: newBlockKey,
focusOffset: 0,
isBackward: false,
}),
});

return EditorState.push(editorState, newContent, 'split-block');
};
body {
font-family: Helvetica, sans-serif;
}

.container-root {
border: 1px solid black;
padding: 5px;
margin: 5px;
}

.my-custom-block {
background-color: cadetblue;
margin: 15px 0;
font-size: 16px;
position: relative;
}

.editable-area {
background-color: lightblue;
height: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.0/Draft.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.7.0/Draft.css" rel="stylesheet"/>
<div id="react-root"></div>
enter image description here

关于javascript - Draft-JS - 如何使用一些不可编辑的文本创建自定义 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com