當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Delta.insert方法代碼示例

本文整理匯總了TypeScript中@typewriter/editor.Delta.insert方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Delta.insert方法的具體用法?TypeScript Delta.insert怎麽用?TypeScript Delta.insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在@typewriter/editor.Delta的用法示例。


在下文中一共展示了Delta.insert方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: embed

 embed(at: number, attributes: { [name: string]: any }) {
   this.updatePosition(at);
   this.delta.insert({ decorator: attributes });
   this.position += 1;
 }
開發者ID:jacwright,項目名稱:typewriter,代碼行數:5,代碼來源:decorators.ts

示例2: deltaFromDom

export function deltaFromDom(root: Element, paper: Paper, options: any = {}) {
  const inDom = root.ownerDocument.contains(root);
  const { blocks, marks, embeds } = paper;
  if (!options) options = defaultOptions;

  var walker = root.ownerDocument.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, {
    acceptNode: node => {
      if (SKIP_ELEMENTS[node.nodeName]) {
        return NodeFilter.FILTER_REJECT;
      } else if (node.nodeType === Node.TEXT_NODE && node.nodeValue === '') {
        node.nodeType === Node.TEXT_NODE;
      } else if (node.nodeType === Node.TEXT_NODE || options.notInDom || inDom) {
        return NodeFilter.FILTER_ACCEPT;
      } else {
        return NodeFilter.FILTER_REJECT;
      }
    }
  });
  const delta = new Delta();
  let currentBlock: any, firstBlockSeen = false, unknownBlock = false, empty = true, node: Node;
  let lastNode = false;

  if (options.startNode) {
    walker.currentNode = options.startNode;
    walker.previousNode();
    if (options.offset) delta.retain(options.offset, undefined);
  } else {
    walker.currentNode = root;
  }

  while ((node = walker.nextNode())) {
    if (node === options.endNode) lastNode = true;
    else if (lastNode) break;

    if (isBRPlaceholder(paper, node)) {
      empty = false;
    } else if (node.nodeType === Node.TEXT_NODE) {
      let parent = node.parentNode as Element;

      // If all newlines, we can ignore
      if (node.nodeValue.replace(/\n+/g, '') === '') continue;

      // non-breaking spaces ( ) are spaces in a delta
      const text = node.nodeValue.replace(/\xA0/g, ' ').replace(/\n+/g, ' ');

      // Word gives us end-of-paragraph nodes with a single space. Ignore them.
      if (!text || (text === ' ' && parent.classList.contains('EOP'))) continue;

      // Gather up all the marks for this text node, walking up to the block level
      const attributes = gatherMarks(parent, root, paper);

      empty = false;
      delta.insert(text, attributes);
    } else if (node.className.indexOf('decorator') !== -1) {
      continue;
    } else if (embeds.matches(node)) {
      const embed = embeds.findByNode(node);
      if (embed) {
        const attributes = gatherMarks(node.parentNode as Element, root, paper);
        delta.insert(embed.fromDom ? embed.fromDom(node, paper) : { [embed.name]: true }, attributes);
      }
    } else if (blocks.matches(node) || (node.nodeType === Node.ELEMENT_NODE && (node as Element).matches(BLOCK_ELEMENTS))) {
      unknownBlock = !blocks.matches(node);

      if (unknownBlock) {
        let parent = node.parentNode;
        while (parent && !blocks.matches(parent) && parent !== root) {
          parent = parent.parentNode;
        }
        // If this block element is inside a recognized block, ignore it
        if (parent && parent !== root) {
          continue;
        }
      }

      const block = blocks.findByNode(node, true);

      // Skip paragraphs/divs inside blockquotes and list items etc.
      if (block === blocks.getDefault() && blocks.matches(node.parentNode)) {
        continue;
      }

      if (firstBlockSeen) {
        if (!currentBlock.unknownBlock || !empty) {
          delta.insert('\n', currentBlock.unknownBlock ? {} : currentBlock);
          empty = true;
        }
      } else {
        firstBlockSeen = true;
      }

      if (unknownBlock) {
        currentBlock = { unknownBlock };
      } else if (block !== blocks.getDefault()) {
        currentBlock = block.fromDom ? block.fromDom(node, paper) : { [block.name]: true };
      } else {
        currentBlock = {};
      }
    }
  }
//.........這裏部分代碼省略.........
開發者ID:jacwright,項目名稱:typewriter,代碼行數:101,代碼來源:delta-dom.ts


注:本文中的@typewriter/editor.Delta.insert方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。