本文整理汇总了TypeScript中draft-js.EditorState.set方法的典型用法代码示例。如果您正苦于以下问题:TypeScript EditorState.set方法的具体用法?TypeScript EditorState.set怎么用?TypeScript EditorState.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类draft-js.EditorState
的用法示例。
在下文中一共展示了EditorState.set方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: removeHighlight
export function removeHighlight(editorState, styleName) {
const highlightsState = getHighlightsState(editorState);
if (highlightsState.highlightsData[styleName] === undefined) {
return editorState;
}
const nextHighlightsStyleMap = {...highlightsState.highlightsStyleMap};
delete nextHighlightsStyleMap[styleName];
const nextHighlightsData = {...highlightsState.highlightsData};
delete nextHighlightsData[styleName];
const newHighlightsState = {
lastHighlightIds: highlightsState.lastHighlightIds,
highlightsStyleMap: nextHighlightsStyleMap,
highlightsData: nextHighlightsData,
};
let newEditorState = clearInlineStyles(
editorState,
getDraftSelectionForEntireContent(editorState),
[styleName],
);
// prevent recording the changes to undo stack so user doesn't have to undo twice
// for the highlight deletion to be completelly(both inline styles and related data) undone
newEditorState = EditorState.set(newEditorState, {allowUndo: false});
newEditorState = setHighlightsState(newEditorState, newHighlightsState);
newEditorState = EditorState.set(newEditorState, {allowUndo: true});
return newEditorState;
}
示例2: addHighlight
export function addHighlight(editorState, type, data, single = false) {
let nextEditorState = editorState;
const initialSelection = nextEditorState.getSelection().merge({
hasFocus: true,
});
const highlightsState = getHighlightsState(nextEditorState);
if (highlightTypeValid(type) !== true) {
throw new Error('Highlight type invalid');
}
if (single && selectionHasType(editorState, type)) {
return editorState;
}
let newIndex = 0;
if (highlightsState.lastHighlightIds && has(highlightsState.lastHighlightIds, type)) {
newIndex = highlightsState.lastHighlightIds[type] + 1;
}
const styleName = type + '-' + newIndex;
const newHighlightsState = {
lastHighlightIds: {
...highlightsState.lastHighlightIds,
[type]: newIndex,
},
highlightsStyleMap: {
...highlightsState.highlightsStyleMap,
[styleName]: availableHighlights[type],
},
highlightsData: {
...highlightsState.highlightsData,
[styleName]: {
...data,
type,
},
},
};
nextEditorState = RichUtils.toggleInlineStyle(nextEditorState, styleName);
// prevent recording the changes to undo stack so user doesn't have to undo twice
// for the highlight to be completelly(both inline styles and related data) undone
nextEditorState = EditorState.set(nextEditorState, {allowUndo: false});
nextEditorState = setHighlightsState(nextEditorState, newHighlightsState);
// make sure the cursor is at the right position after undo
// it used to always end up at the position 0 of the first block
// when undoing after changing block data with `allowUndo` set to false
nextEditorState = EditorState.push(
nextEditorState,
nextEditorState.getCurrentContent().set('selectionBefore', initialSelection),
'change-block-data',
);
// restore focus lost after clicking a toolbar action or entering highlight data OR pushing editorState
// so the selection is visible after undo
nextEditorState = EditorState.acceptSelection(
nextEditorState,
initialSelection,
);
nextEditorState = EditorState.set(nextEditorState, {allowUndo: true});
return nextEditorState;
}