immutable#OrderedSet TypeScript Examples

The following examples show how to use immutable#OrderedSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: htmlToDraft.ts    From brilliant with MIT License 6 votes vote down vote up
function getWhitespaceChunk(inEntity) {
  const entities = new Array(1);
  if (inEntity) {
    entities[0] = inEntity;
  }
  return {
    text: SPACE,
    inlines: [OrderedSet()],
    entities,
    blocks: [],
  };
}
Example #2
Source File: htmlToDraft.ts    From brilliant with MIT License 6 votes vote down vote up
function getSoftNewlineChunk(block, depth, flat = false, data = Map()) {
  if (flat === true) {
    return {
      text: '\r',
      inlines: [OrderedSet()],
      entities: new Array(1),
      blocks: [
        {
          type: block,
          data,
          depth: Math.max(0, Math.min(MAX_DEPTH, depth)),
        },
      ],
      isNewline: true,
    };
  }

  return {
    text: '\n',
    inlines: [OrderedSet()],
    entities: new Array(1),
    blocks: [],
  };
}
Example #3
Source File: htmlToDraft.ts    From brilliant with MIT License 6 votes vote down vote up
function getBlockDividerChunk(block, depth, data = Map()) {
  return {
    text: '\r',
    inlines: [OrderedSet()],
    entities: new Array(1),
    blocks: [
      {
        type: block,
        data,
        depth: Math.max(0, Math.min(MAX_DEPTH, depth)),
      },
    ],
  };
}
Example #4
Source File: htmlToDraft.ts    From brilliant with MIT License 5 votes vote down vote up
function baseProcessInlineTag(tag, node, inlineStyles = OrderedSet()) {
  return processInlineTag(tag, node, inlineStyles);
}
Example #5
Source File: htmlToDraft.ts    From brilliant with MIT License 4 votes vote down vote up
function genFragment(
  node: { nodeName: string; textContent: any; firstChild: any },
  inlineStyle: OrderedSet<unknown>,
  lastList: string,
  inBlock: string,
  fragmentBlockTags: string | any[],
  depth: number,
  processCustomInlineStyles: (arg0: any, arg1: any, arg2: any) => any,
  checkEntityNode: (
    arg0: any,
    arg1: any,
    arg2: any,
    arg3: any,
    arg4: any,
    arg5: any
  ) => any,
  checkEntityText: any,
  checkBlockType: (arg0: any, arg1: any, arg2: any, arg3: any) => any,
  createEntity: any,
  getEntity: any,
  mergeEntityData: any,
  replaceEntityData: any,
  options: { flat: boolean },
  inEntity: string
) {
  let nodeName = node.nodeName.toLowerCase();
  let newBlock = false;
  let nextBlockType = 'unstyled';

  if (nodeName === '#text') {
    let text = node.textContent;
    if (text.trim() === '' && inBlock === null) {
      return getEmptyChunk();
    }

    if (text.trim() === '' && inBlock !== 'code-block') {
      return getWhitespaceChunk(inEntity);
    }
    if (inBlock !== 'code-block') {
      text = text.replace(REGEX_LF, SPACE);
    }

    const entities = Array(text.length).fill(inEntity);

    let offsetChange = 0;
    const textEntities = checkEntityText(
      text,
      createEntity,
      getEntity,
      mergeEntityData,
      replaceEntityData
    ).sort(rangeSort);

    textEntities.forEach(({ entity, offset, length, result }) => {
      const adjustedOffset = offset + offsetChange;

      if (result === null || result === undefined) {
        result = text.substr(adjustedOffset, length);
      }

      const textArray = text.split('');
      textArray.splice
        .bind(textArray, adjustedOffset, length)
        .apply(textArray, result.split(''));
      text = textArray.join('');

      entities.splice
        .bind(entities, adjustedOffset, length)
        .apply(entities, Array(result.length).fill(entity));
      offsetChange += result.length - length;
    });

    return {
      text,
      inlines: Array(text.length).fill(inlineStyle),
      entities,
      blocks: [],
    };
  }

  if (nodeName === 'br') {
    const blockType = inBlock;

    if (blockType === null) {
      return getSoftNewlineChunk('unstyled', depth, true);
    }

    return getSoftNewlineChunk(blockType || 'unstyled', depth, options.flat);
  }

  let chunk = getEmptyChunk();
  let newChunk = null;

  inlineStyle = processInlineTag(nodeName, node, inlineStyle);
  inlineStyle = processCustomInlineStyles(nodeName, node, inlineStyle);

  if (nodeName === 'ul' || nodeName === 'ol') {
    if (lastList) {
      depth += 1;
    }
    lastList = nodeName;
    inBlock = null;
  }

  let blockInfo = checkBlockType(nodeName, node, lastList, inBlock);
  let blockType;
  let blockDataMap;

  if (blockInfo === false) {
    return getEmptyChunk();
  }

  blockInfo = blockInfo || {};

  if (typeof blockInfo === 'string') {
    blockType = blockInfo;
    blockDataMap = Map();
  } else {
    blockType = typeof blockInfo === 'string' ? blockInfo : blockInfo.type;
    blockDataMap = blockInfo.data ? Map(blockInfo.data) : Map();
  }
  if (!inBlock && (fragmentBlockTags.indexOf(nodeName) !== -1 || blockType)) {
    if(getBlockTypeForTag(nodeName, lastList)==='div'){
    }
    else{
    chunk = getBlockDividerChunk(
      blockType || getBlockTypeForTag(nodeName, lastList),
      depth,
      blockDataMap
    );
    inBlock = blockType || getBlockTypeForTag(nodeName, lastList);
    newBlock = true;
    }
  } else if (
    lastList &&
    (inBlock === 'ordered-list-item' || inBlock === 'unordered-list-item') &&
    nodeName === 'li'
  ) {
    const listItemBlockType = getBlockTypeForTag(nodeName, lastList);
    chunk = getBlockDividerChunk(listItemBlockType, depth);
    inBlock = listItemBlockType;
    newBlock = true;
    nextBlockType =
      lastList === 'ul' ? 'unordered-list-item' : 'ordered-list-item';
  } else if (inBlock && inBlock !== 'atomic' && blockType === 'atomic') {
    inBlock = blockType;
    newBlock = true;
    chunk = getSoftNewlineChunk(blockType, depth, true, blockDataMap);
  }
  let child = node.firstChild;

  if (
    child == null &&
    inEntity &&
    (blockType === 'atomic' || inBlock === 'atomic')
  ) {
    child = document.createTextNode(' ');
  }

  if (child != null) {
    nodeName = child.nodeName.toLowerCase();
  }

  let entityId = null;

  while (child) {
    entityId = checkEntityNode(
      nodeName,
      child,
      createEntity,
      getEntity,
      mergeEntityData,
      replaceEntityData
    );

    newChunk = genFragment(
      child,
      inlineStyle,
      lastList,
      inBlock,
      fragmentBlockTags,
      depth,
      processCustomInlineStyles,
      checkEntityNode,
      checkEntityText,
      checkBlockType,
      createEntity,
      getEntity,
      mergeEntityData,
      replaceEntityData,
      options,
      entityId || inEntity
    );

    chunk = joinChunks(chunk, newChunk, options.flat);
    const sibling = child.nextSibling;

    if (sibling && fragmentBlockTags.indexOf(nodeName) >= 0 && inBlock) {
      let newBlockInfo = checkBlockType(nodeName, child, lastList, inBlock);

      let newBlockType;
      let newBlockData;

      if (newBlockInfo !== false) {
        newBlockInfo = newBlockInfo || {};

        if (typeof newBlockInfo === 'string') {
          newBlockType = newBlockInfo;
          newBlockData = Map();
        } else {
          newBlockType =
            newBlockInfo.type || getBlockTypeForTag(nodeName, lastList);
          newBlockData = newBlockInfo.data ? Map(newBlockInfo.data) : Map();
        }

        chunk = joinChunks(
          chunk,
          getSoftNewlineChunk(newBlockType, depth, options.flat, newBlockData),
          options.flat
        );
      }
    }
    if (sibling) {
      nodeName = sibling.nodeName.toLowerCase();
    }
    child = sibling;
  }

  if (newBlock) {
    chunk = joinChunks(
      chunk,
      getBlockDividerChunk(nextBlockType, depth, Map()),
      options.flat
    );
  }

  return chunk;
}
Example #6
Source File: htmlToDraft.ts    From brilliant with MIT License 4 votes vote down vote up
function getChunkForHTML(
  html: string,
  processCustomInlineStyles: (arg0: any, arg1: any, arg2: any) => any,
  checkEntityNode: (
    arg0: any,
    arg1: any,
    arg2: any,
    arg3: any,
    arg4: any,
    arg5: any
  ) => any,
  checkEntityText: (
    arg0: any,
    arg1: any,
    arg2: any,
    arg3: any,
    arg4: any
  ) => { offset: number; length: number }[],
  checkBlockType: (arg0: any, arg1: any, arg2: any, arg3: any) => any,
  createEntity: any,
  getEntity: any,
  mergeEntityData: any,
  replaceEntityData: any,
  options: { flat: boolean },
  DOMBuilder: (arg0: any) => any
) {
  html = html
    .trim()
    .replace(REGEX_CR, '')
    .replace(REGEX_NBSP, SPACE);

  const safeBody = DOMBuilder(html);
  if (!safeBody) {
    return null;
  }

  const workingBlocks = containsSemanticBlockMarkup(html)
    ? blockTags.concat(['div'])
    : ['div'];

  let chunk = genFragment(
    safeBody,
    OrderedSet(),
    'ul',
    null,
    workingBlocks,
    -1,
    processCustomInlineStyles,
    checkEntityNode,
    checkEntityText,
    checkBlockType,
    createEntity,
    getEntity,
    mergeEntityData,
    replaceEntityData,
    options,
    undefined
  );
  if (chunk.text.indexOf('\r') === 0) {
    chunk = {
      text: chunk.text.slice(1),
      inlines: chunk.inlines.slice(1),
      entities: chunk.entities.slice(1),
      blocks: chunk.blocks,
    };
  }

  if (chunk.text.slice(-1) === '\r') {
    chunk.text = chunk.text.slice(0, -1);
    chunk.inlines = chunk.inlines.slice(0, -1);
    chunk.entities = chunk.entities.slice(0, -1);
    chunk.blocks.pop();
  }

  if (chunk.blocks.length === 0) {
    chunk.blocks.push({ type: 'unstyled', data: Map(), depth: 0 });
  }

  if (chunk.text.split('\r').length === chunk.blocks.length + 1) {
    chunk.blocks.unshift({ type: 'unstyled', data: Map(), depth: 0 });
  }

  return chunk;
}