当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript EditorState.getSelection方法代码示例

本文整理汇总了TypeScript中draft-js.EditorState.getSelection方法的典型用法代码示例。如果您正苦于以下问题:TypeScript EditorState.getSelection方法的具体用法?TypeScript EditorState.getSelection怎么用?TypeScript EditorState.getSelection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在draft-js.EditorState的用法示例。


在下文中一共展示了EditorState.getSelection方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1:

export const removeBlocks = (editorState: EditorState) => {
  return Modifier.setBlockType(
    editorState.getCurrentContent(),
    editorState.getSelection(),
    "unstyled"
  )
}
开发者ID:joeyAghion,项目名称:positron,代码行数:7,代码来源:utils.ts

示例2: getCurrentEntity

export default function getCurrentEntity(editorState: EditorState) {
  let entity;
  const selection = editorState.getSelection();
  let start = selection.getStartOffset();
  let end = selection.getEndOffset();
  if (start === end && start === 0) {
    end = -1;
  } else if (start === end) {
    start = start - 1;
  }
  const block = getSelectedBlock(editorState);
  for (let i = start; i < end; i++) {
    const currentEntity = block.getEntityAt(i);
    if (!currentEntity) {
      entity = undefined;
      break;
    }
    if (i === start) {
      entity = currentEntity;
    } else {
      if (entity !== currentEntity) {
        entity = undefined;
        break;
      }
    }
  }
  return entity;
}
开发者ID:react-component,项目名称:editor-utils,代码行数:28,代码来源:getCurrentEntity.ts

示例3:

export const getSelectionDetails = (editorState: EditorState) => {
  // Returns some commonly used selection attrs
  const selection = editorState.getSelection()
  const content = editorState.getCurrentContent()

  const anchorKey = selection.getAnchorKey()
  const anchorOffset = selection.getAnchorOffset()
  const anchorBlock = content.getBlockForKey(anchorKey)
  const anchorType = anchorBlock.getType()

  const beforeKey = content.getKeyBefore(anchorKey)
  const blockBefore = content.getBlockForKey(beforeKey)

  const isFirstBlock = !blockBefore
  const isFirstCharacter = selection.getStartOffset() === 0
  const isLastBlock = content.getLastBlock().getKey() === anchorKey
  const isLastCharacter = selection.getStartOffset() === anchorBlock.getLength()

  return {
    anchorKey,
    anchorOffset,
    anchorType,
    isFirstBlock,
    isFirstCharacter,
    isLastBlock,
    isLastCharacter,
  }
}
开发者ID:artsy,项目名称:positron,代码行数:28,代码来源:selection.ts

示例4: 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
        );
    }
开发者ID:DevenLu,项目名称:YourFukurou,代码行数:33,代码来源:tweet_editor.ts

示例5: 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
}
开发者ID:Pajn,项目名称:meltdown,代码行数:43,代码来源:editor-syntax-highlighting.ts

示例6:

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")
}
开发者ID:artsy,项目名称:positron,代码行数:15,代码来源:shared.ts

示例7: 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
}
开发者ID:Pajn,项目名称:meltdown,代码行数:36,代码来源:document.ts

示例8: 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';
}
开发者ID:jerome-poisson,项目名称:superdesk-client-core,代码行数:74,代码来源:handlePastedText.ts

示例9: getSelectedBlock

export default function getSelectedBlock(editorState: EditorState): ContentBlock {
  const currentContent = editorState.getCurrentContent()
  const selection = editorState.getSelection()
  return currentContent.getBlockForKey(selection.getStartKey())
}
开发者ID:react-component,项目名称:editor-utils,代码行数:5,代码来源:getSelectedBlock.ts


注:本文中的draft-js.EditorState.getSelection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。