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


TypeScript source-mutation.createStringMutation函數代碼示例

本文整理匯總了TypeScript中source-mutation.createStringMutation函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createStringMutation函數的具體用法?TypeScript createStringMutation怎麽用?TypeScript createStringMutation使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


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

示例1: parseModuleSource

export const editPaperclipSource = (content: string, mutation: Mutation<PCExpression>): StringMutation[] => {

  const result = parseModuleSource(content);

  // source is deprecated
  const targetNode = findTargetNode(result.root, mutation.target.location || mutation.target["source"]);

  if (mutation.target["source"]) {
    console.warn(`Mutation target does not match expected type.`);
  }

  if (!targetNode) {
    return [createStringMutation(0, 0, ``)]
  }
  
  // switch(mutation.type) {
  //   case UPDATE_VALUE_NODE: {
  //     return editNodeValue(targetNode as PCTextNode, mutation as SetValueMutation<any>);
  //   }
  //   case SET_ELEMENT_ATTRIBUTE_EDIT: {
  //     return editElementAttribute(targetNode, mutation as SetPropertyMutation<any>);
  //   }
  //   case REMOVE_CHILD_NODE_EDIT: {
  //     return removeChildAt(targetNode as PCParent, (mutation as RemoveChildMutation<any, any>).index);
  //   }
  //   case INSERT_CHILD_NODE_EDIT: {
  //     return insertChild(targetNode as PCParent, mutation as RemoveChildMutation<any, any>, content);
  //   }
  //   case INSERT_HTML_EDIT: {
  //     return insertHTML(targetNode as PCParent, mutation as InsertHTMLMutation<any>, content);
  //   }

  //   case PC_REMOVE_CHILD_NODE: {
  //     return removeChildAt(targetNode as PCParent, (mutation as PCRemoveChildNodeMutation).index);
  //   }

  //   case PC_REMOVE_NODE: {
  //     return removeNode(targetNode);
  //   }
    
  //   case CSS_INSERT_CSS_RULE_TEXT: {
  //     // TODO
  //   }
  //   case CSS_PARENT_DELETE_RULE: {
  //     // TODO
  //   }
  //   case CSS_STYLE_RULE_SET_SELECTOR_TEXT: {
  //     // TODO
  //   }
  //   case CSS_STYLE_RULE_SET_STYLE_PROPERTY: {
  //     // TODO
  //   }
  // }

  return [createStringMutation(0, 0, ``)];
};
開發者ID:cryptobuks,項目名稱:tandem,代碼行數:56,代碼來源:edit.ts

示例2:

const insertHTML = (parent: PCParent, { childIndex, html }: any, source: string) => {
  if (parent.type === PCExpressionType.SELF_CLOSING_ELEMENT) {
    const parentElement = parent as any as PCSelfClosingElement;
    return [createStringMutation(eatWhitespace(parentElement.location.end.pos - 2, source), parentElement.location.end.pos, ">" + html + `</${parentElement.name}>`)];
  }
  const beforeChild = parent.childNodes[childIndex];
  if (!beforeChild) {
    const parentElement = parent as PCElement;
    return [createStringMutation(parentElement.startTag.location.end.pos, parentElement.startTag.location.end.pos, html)];
  }
  return [createStringMutation(beforeChild.location.start.pos, beforeChild.location.start.pos, html)];
}
開發者ID:cryptobuks,項目名稱:tandem,代碼行數:12,代碼來源:edit.ts

示例3: if

const editElementAttribute = (target: PCExpression, mutation: SetPropertyMutation<any>) => {
  
  let startTag: PCSelfClosingElement | PCStartTag;

  if (target.type === PCExpressionType.SELF_CLOSING_ELEMENT) {
    startTag = target as PCSelfClosingElement;
  } else if (target.type === PCExpressionType.ELEMENT) {
    startTag = (target as PCElement).startTag;
  }

  let found;
  
  let mutations: StringMutation[] = [];

  const mutateAttrName = mutation.name;
  const mutateAttrValue = mutation.newValue && `"${mutation.newValue}"`;

  for (const attribute of startTag.attributes) {

    // TODO - need to consider spreads
    const attr = attribute as PCAttribute;
    if (attr.name === mutateAttrName) {
      found = true;

      // if the attribute value is undefined, then remove it
      if (!mutateAttrValue) {

        // remove the whitespace before attribute
        return [createStringMutation(attr.location.start.pos - 1, attr.location.end.pos, ``)];
      } else {
        return [createStringMutation(attr.value.location.start.pos, attr.value.location.end.pos, mutateAttrValue)];
      }
    }
  }

  if (!found) {
    const insertIndex = startTag.location.start.pos + startTag.name.length + 1;
    return [createStringMutation(insertIndex, insertIndex, mutation.newValue === true ? mutateAttrName : ` ${mutateAttrName}=${mutateAttrValue}`)];
  }
};
開發者ID:cryptobuks,項目名稱:tandem,代碼行數:40,代碼來源:edit.ts

示例4: createStringMutation

const editStyleRuleDeclaration = (target: CSSStyleRule, { newValue }: SetPropertyMutation<any>) => {

  const prettyStyleText = newValue.split(/\s*;\s*/g).join(";\n");

  // not tested
  return createStringMutation(
    target.location.start.pos,
    target.location.end.pos + 1,
    `${target.selectorText.trim()} {
      ${prettyStyleText}
    }`
  );
}
開發者ID:cryptobuks,項目名稱:tandem,代碼行數:13,代碼來源:edit.ts


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