本文整理汇总了TypeScript中draft-js.EditorState.push方法的典型用法代码示例。如果您正苦于以下问题:TypeScript EditorState.push方法的具体用法?TypeScript EditorState.push怎么用?TypeScript EditorState.push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类draft-js.EditorState
的用法示例。
在下文中一共展示了EditorState.push方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: toggleBlockStyle
return function toggleBlockStyle(styleName: string) {
const { getEditorState, setEditorState } = callbacks;
const editorState = getEditorState();
const currentBlock = getSelectedBlock(editorState);
const blockTypedContent = Modifier.setBlockType(
editorState.getCurrentContent(),
editorState.getSelection(),
styleName
);
setEditorState(
EditorState.push(editorState, blockTypedContent, 'apply-block-type')
);
}
示例2: flipIfBackwards
.when(createComment, (state: DocumentState, {text}) => {
const selection = flipIfBackwards(state.editor.getSelection())
const id = state.comments.length
const entityKey = Entity.create('Comment', 'MUTABLE', {id})
let content = state.editor.getCurrentContent()
if (selection.getAnchorKey() === selection.getFocusKey()) {
const start = selection.getAnchorOffset()
const end = selection.getFocusOffset()
let block = content.getBlockForKey(selection.getAnchorKey())
let chars = block.getCharacterList()
for (let i = start; i < end; i++) {
let metadata = chars.get(i)
metadata = CharacterMetadata.applyEntity(metadata, entityKey)
metadata = CharacterMetadata.applyStyle(metadata, 'comment')
chars = chars.set(i, metadata)
}
block = block.set('characterList', chars)
content = content.set('blockMap', content.getBlockMap().set(block.getKey(), block))
}
const comment = {id, entityKey, selection, text}
return Object.assign({}, state, {
editor: EditorState.push(state.editor, content, 'apply-entity'),
creatingComment: null,
comments: [...state.comments, comment],
})
})
示例3: onSelect
onSelect(query: string, text: string) {
const selection = this.core.getSelection();
const offset = selection.getAnchorOffset() - 1;
const content = this.core.getCurrentContent();
const block_text = content.getBlockForKey(selection.getAnchorKey()).getText();
const idx = block_text.lastIndexOf(query, offset);
if (idx === -1 || (idx + query.length < offset)) {
log.error('Invalid selection:', selection);
return this;
}
const next_selection = selection.merge({
anchorOffset: idx,
focusOffset: idx + query.length,
}) as SelectionState;
const next_content = Modifier.replaceText(content, next_selection, text);
const next_editor = EditorState.forceSelection(
EditorState.push(
this.core,
next_content,
'insert-characters'
),
next_content.getSelectionAfter()
);
return new TweetEditorState(
next_editor,
this.is_open,
this.keymaps,
this.in_reply_to_status
);
}
示例4: setTextToEditor
setTextToEditor(text: string) {
return EditorState.moveSelectionToEnd(
EditorState.push(
this.core,
ContentState.createFromText(text),
'insert-characters'
)
);
}
示例5: addHighlight
(start, end) => {
editorState = EditorState.acceptSelection(editorState, selection.merge({
isBackward: false,
anchorOffset: start,
focusOffset: end,
}));
editorState = addHighlight(editorState, type, data, single);
editorState = EditorState.push(editorState, editorState.getCurrentContent(), 'change-block-data');
editorState = EditorState.acceptSelection(editorState, selection);
});
示例6:
export const removeInlineStyles = (editorState: EditorState) => {
let contentState = editorState.getCurrentContent()
draftDefaultStyles.forEach((style: string) => {
contentState = Modifier.removeInlineStyle(
contentState,
editorState.getSelection(),
style
)
})
return EditorState.push(editorState, contentState, "change-inline-style")
}
示例7: closeEditor
closeEditor() {
GlobalKeyMaps.enable();
return new TweetEditorState(
EditorState.push(
this.core,
ContentState.createFromText(''),
'remove-range'
),
false,
this.keymaps,
null
);
}
示例8: modifyInlineStyle
export function modifyInlineStyle(
editor: EditorState, selection: SelectionState, {apply, remove}: {apply?: string, remove?: string}
) {
let content = editor.getCurrentContent()
if (remove) {
content = Modifier.removeInlineStyle(content, selection, remove)
}
if (apply) {
content = Modifier.applyInlineStyle(content, selection, apply)
}
return EditorState.push(editor, content, 'change-inline-style')
}
示例9: removeLinks
export const makePlainText = (editorState: EditorState) => {
// Remove links
const editorStateWithoutLinks = removeLinks(editorState)
// Remove inline styles
const editorStateWithoutStyles = removeInlineStyles(editorStateWithoutLinks)
// Remove blocks from selection
const contentStateWithoutBlocks = removeBlocks(editorStateWithoutStyles)
// Merge existing and stripped states
return EditorState.push(
editorState,
contentStateWithoutBlocks,
"change-inline-style"
)
}
示例10: updateComments
function updateComments(state: DocumentState, editor: EditorState) {
const content = editor.getCurrentContent()
const selection = editor.getSelection()
const commentId = getCurrentComment(selection, content)
let updatedState
if (typeof commentId === 'number') {
if (!state.focusedComment || commentId !== state.focusedComment.id) {
const focusedComment = state.comments[commentId]
let updatedContent = content
if (state.focusedComment) {
updatedContent = changeCommentFocus(updatedContent, state.focusedComment, {focus: false})
}
updatedContent = changeCommentFocus(updatedContent, focusedComment, {focus: true})
updatedState = Object.assign({}, state, {
editor: EditorState.push(editor, updatedContent, 'change-inline-style'),
focusedComment,
})
}
} else if (state.focusedComment) {
const updatedContent = changeCommentFocus(content, state.focusedComment, {focus: false})
updatedState = Object.assign({}, state, {
editor: EditorState.push(editor, updatedContent, 'change-inline-style'),
focusedComment: null,
})
}
if (!updatedState) {
updatedState = updateIn('editor', editor, state)
}
return updatedState
}
示例11:
export const insertPastedState = (
pastedState: EditorState,
editorState: EditorState
) => {
const blockMap = pastedState.getCurrentContent().getBlockMap()
// Merge blockMap from pasted text into existing content
const modifiedContent = Modifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
blockMap
)
// Create a new editorState from merged content
return EditorState.push(editorState, modifiedContent, "insert-fragment")
}
示例12: changeCommentFocus
.when(focusComment, (state, {id}) => {
const comment = state.comments[id]
const content = state.editor.getCurrentContent()
if (state.focusedComment === comment) return state
let updatedContent = changeCommentFocus(content, comment, {focus: true})
if (state.focusedComment) {
updatedContent = changeCommentFocus(updatedContent, state.focusedComment, {focus: false})
}
return Object.assign({}, state, {
editor: EditorState.push(state.editor, updatedContent, 'change-inline-style'),
focusedComment: comment,
})
})
示例13: updateStyles
export function updateStyles(editor: EditorState) {
const key = editor.getSelection().getFocusKey()
const content = editor.getCurrentContent()
const block = content.getBlockForKey(key)
const ast = parser.parse(block.getText())
let updatedBlock: ContentBlock
let updatedContent: ContentState
if (ast.firstChild === ast.lastChild && ast.firstChild) {
const node = ast.firstChild
if (node.type === 'Heading') {
const type = headerType[ast.firstChild.level] || 'unstyled'
updatedBlock = maybeSet(block, type)
} else if (node.type === 'BlockQuote') {
updatedBlock = maybeSet(block, 'blockquote')
// } else if (node.type === 'Paragraph' && node.firstChild !== node.lastChild) {
// updatedContent = updateInlineStyles(content, block.getKey(), node)
} else {
updatedBlock = maybeSet(block, 'unstyled')
}
/// goal question metiric, victor basili
/// Program slicing, component based systems
} else {
updatedBlock = maybeSet(block, 'unstyled')
}
if (updatedBlock) {
updatedContent = content.setIn(['blockMap', key], updatedBlock)
}
if (updatedContent) {
const selection = editor.getSelection()
updatedContent = updatedContent.merge({
selectionBefore: selection,
selectionAfter: selection,
})
return EditorState.push(editor, updatedContent, 'change-block-type')
}
return editor
}
示例14: insertContentInState
function insertContentInState(
editorState: EditorState,
pastedContent: ContentState,
onChange: (e: EditorState) => void,
editorFormat: Array<string>): DraftHandleValue {
let _pastedContent = pastedContent;
const blockMap = _pastedContent.getBlockMap();
const hasAtomicBlocks = blockMap.some((block) => block.getType() === 'atomic');
const acceptedInlineStyles =
Object.keys(inlineStyles)
.filter((style) => editorFormat.includes(style))
.map((style) => inlineStyles[style]);
let contentState = editorState.getCurrentContent();
let selection = editorState.getSelection();
let blocks = [];
if (hasAtomicBlocks) {
contentState = Modifier.splitBlock(editorState.getCurrentContent(), editorState.getSelection());
selection = contentState.getSelectionAfter();
}
_pastedContent = sanitizeContent(EditorState.createWithContent(_pastedContent), acceptedInlineStyles)
.getCurrentContent();
blockMap.forEach((block) => {
if (!hasAtomicBlocks || block.getType() !== 'atomic') {
return blocks.push(block);
}
const entityKey = block.getEntityAt(0);
const entity = _pastedContent.getEntity(entityKey);
contentState = contentState.addEntity(entity);
blocks = blocks.concat(
atomicBlock(block.getData(), contentState.getLastCreatedEntityKey()),
);
});
if (hasAtomicBlocks) {
contentState = Modifier.setBlockType(contentState, selection, 'atomic');
blocks = blocks.concat(emptyBlock()); // add empty block to ensure writting afterwards
}
const newBlockMap = OrderedMap<string, ContentBlock>(blocks.map((b) => ([b.getKey(), b])));
let nextEditorState = EditorState.push(
editorState,
Modifier.replaceWithFragment(contentState, selection, newBlockMap),
'insert-fragment',
);
const selectionAfterInsert = nextEditorState.getSelection();
const customData = getAllCustomDataFromEditor(editorState);
// for the first block recover the initial block data because on replaceWithFragment the block data is
// replaced with the data from pasted fragment
nextEditorState = setAllCustomDataForEditor(nextEditorState, customData);
// reset undo stack
nextEditorState = EditorState.push(
editorState,
nextEditorState.getCurrentContent(),
'insert-fragment',
);
nextEditorState = EditorState.forceSelection(nextEditorState, selectionAfterInsert);
onChange(nextEditorState);
return 'handled';
}