Example #1
0
 (lastPos) => {
   return Options.liftN([Arr.head(lastPos.getClientRects()), Arr.head(newPos.getClientRects())], (lastRect, newRect) => {
     const lastDist = Math.abs(x - lastRect.left);
     const newDist = Math.abs(x - newRect.left);
     return newDist <= lastDist ? newPos : lastPos;
   }).or(acc);
 }
Example #2
0
const changeFileInput = (helpers: Helpers, info: ImageDialogInfo, state: ImageDialogState, api: API) => {
  const data = api.getData();
  api.block('Uploading image'); // What msg do we pass to the lock?
  Arr.head(data.fileinput)
    .fold(() => {
      api.unblock();
    }, (file) => {
      const blobUri: string = URL.createObjectURL(file);

      const uploader = Uploader({
        url: info.url,
        basePath: info.basePath,
        credentials: info.credentials,
        handler: info.handler
      });

      const finalize = () => {
        api.unblock();
        URL.revokeObjectURL(blobUri);
      };

      Utils.blobToDataUri(file).then((dataUrl) => {
        const blobInfo = helpers.createBlobCache(file, blobUri, dataUrl);
        uploader.upload(blobInfo).then((url: string) => {
          api.setData({ src: { value: url, meta: { } } });
          api.showTab('General');
          changeSrc(helpers, info, state, api);
          finalize();
        }).catch((err) => {
          finalize();
          helpers.alertErr(api, err);
        });
      });
    });
};
Example #3
0
const composeList = (scope: Document, entries: Entry[]): Option<Element> => {
  const cast: Segment[] = Arr.foldl(entries, (cast, entry) => {
    return entry.depth > cast.length ? writeDeep(scope, cast, entry) : writeShallow(scope, cast, entry);
  }, []);

  return Arr.head(cast).map((segment) => segment.list);
};
Example #4
0
const writeDeep = (scope: Document, outline: Section[], entry: Entry): Section[] => {
  const newSections = createJoinedSections(scope, entry.depth - outline.length, entry.listType);
  populateSections(newSections, entry);

  Options.liftN([
    Arr.last(outline),
    Arr.head(newSections)
  ], joinSections);
  return outline.concat(newSections);
};
Example #5
0
const scrollRangeIntoView = (editor: Editor, rng: Range) => {
  Arr.head(CaretPosition.fromRangeStart(rng).getClientRects()).each((rngRect) => {
    const bodyRect = getViewPortRect(editor);
    const overflow = getOverflow(bodyRect, rngRect);
    const margin = 4;
    const dx = overflow.x > 0 ? overflow.x + margin : overflow.x - margin;
    const dy = overflow.y > 0 ? overflow.y + margin : overflow.y - margin;

    scrollBy(editor, overflow.x !== 0 ? dx : 0, overflow.y !== 0 ? dy : 0);
  });
};
Example #6
0
const getMaxHeight = (heights: number[]) => {
  return Arr.head(Arr.sort(heights, (a, b) => {
    if (a > b) {
      return -1;
    } else if (a < b) {
      return +1;
    } else {
      return 0;
    }
  }));
};
Example #7
0
 return () => {
   const from = forward ? CaretPosition.fromRangeEnd(editor.selection.getRng()) : CaretPosition.fromRangeStart(editor.selection.getRng());
   const result = forward ? getPositionsUntilNextLine(editor.getBody(), from) : getPositionsUntilPreviousLine(editor.getBody(), from);
   const to = forward ? Arr.last(result.positions) : Arr.head(result.positions);
   return to.filter(isCefPosition(forward)).fold(
     Fun.constant(false),
     (pos) => {
       editor.selection.setRng(pos.toRange());
       return true;
     }
   );
 };
Example #8
0
const isAtBlockBoundary = (forward: boolean, root: Element, pos: CaretPosition) => {
  const parentBlocks = Arr.filter(Parents.parentsAndSelf(Element.fromDom(pos.container()), root), ElementType.isBlock);
  return Arr.head(parentBlocks).fold(
    () => {
      return navigateIgnoreEmptyTextNodes(forward, root.dom(), pos).forall((newPos) => {
        return isInSameBlock(newPos, pos, root.dom()) === false;
      });
    },
    (parent) => {
      return navigateIgnoreEmptyTextNodes(forward, parent.dom(), pos).isNone();
    }
  );
};
Example #9
0
const open = (editor: Editor) => {
  const languages: LanguageSpec[] = Languages.getLanguages(editor);
  const defaultLanguage: string = Arr.head(languages).fold(() => '', (l) => l.value);
  const currentLanguage: string = Languages.getCurrentLanguage(editor, defaultLanguage);
  const currentCode: string = CodeSample.getCurrentCode(editor);

  editor.windowManager.open({
    title: 'Insert/Edit Code Sample',
    size: 'large',
    body: {
      type: 'panel',
      items: [
        {
          type: 'selectbox',
          name: 'language',
          label: 'Language',
          items: languages
        },
        {
          type: 'textarea',
          name: 'code',
          label: 'Code view',
        }
      ]
    },
    buttons: [
      {
        type: 'cancel',
        name: 'cancel',
        text: 'Cancel',
      },
      {
        type: 'submit',
        name: 'save',
        text: 'Save',
        primary: true
      }
    ],
    initialData: {
      language: currentLanguage,
      code: currentCode
    },
    onSubmit: (api) => {
      const data = api.getData();
      CodeSample.insertCodeSample(editor, data.language, data.code);
      api.close();
    }
  });
};
Example #10
0
 find: (comp: AlloyComponent) => {
   const children = Replacing.contents(comp);
   return Arr.head(children);
 }
 () => endCell.bind((endCell) => getClosestTable(endCell, isRoot).bind((table) => {
   return Arr.head(getTableCells(table)).map((startCell) => tableCellRng(startCell, endCell));
 })),
Example #12
0
const showTab = (allTabs, comp: AlloyComponent) => {
  Arr.head(allTabs).each((tab) => TabSection.showTab(comp, tab.value));
};
Example #13
0
 () => {
   return Arr.head(start.getClientRects()).bind((rect) => {
     return findClosestHorizontalPositionFromPoint(getPositionsBelow(root, CaretPosition.after(table)), rect.left);
   });
 }
Example #14
0
const appendSegments = (head: Segment[], tail: Segment[]): void => {
  Options.liftN([ Arr.last(head), Arr.head(tail)], joinSegment);
};
Example #15
0
const findClosestPositionInAboveCell = (table: HTMLElement, pos: CaretPosition): Option<CaretPosition> => {
  return Arr.head(pos.getClientRects()).bind((rect) => {
    return getClosestCellAbove(table, rect.left, rect.top);
  }).bind((cell) => findClosestHorizontalPosition(getLastLinePositions(cell), pos));
};
Example #16
0
 return Arr.bind(Arr.groupBy(entries, isIndented), (entries) => {
   const groupIsIndented = Arr.head(entries).map(isIndented).getOr(false);
   return groupIsIndented ? indentedComposer(editor, entries) : outdentedComposer(editor, entries);
 });
Example #17
0
 find: (comp) => Arr.head(AlloyTabSection.getViewItems(comp))
Example #18
0
const composeList = (scope: Document, entries: Entry[]): Option<Element> => {
  const outline: Section[] = Arr.foldl(entries, (outline, entry) => {
    return entry.depth > outline.length ? writeDeep(scope, outline, entry) : writeShallow(scope, outline, entry);
  }, []);
  return Arr.head(outline).map((section) => section.list);
};
Example #19
0
const isBlockFormatName = (name: string, formatter: Formatter) => {
  const formatSet = formatter.get(name);
  return Type.isArray(formatSet) && Arr.head(formatSet).exists((format) => Obj.has(format as any, 'block'));
};
Example #20
0
const findClosestHorizontalPosition = (positions: CaretPosition[], pos: CaretPosition): Option<CaretPosition> => {
  return Arr.head(pos.getClientRects()).bind((targetRect) => {
    return findClosestHorizontalPositionFromPoint(positions, targetRect.left);
  });
};
Example #21
0
const findBr = (forward: boolean, root: Element, pos: CaretPosition) => {
  const parentBlocks = Arr.filter(Parents.parentsAndSelf(Element.fromDom(pos.container()), root), ElementType.isBlock);
  const scope = Arr.head(parentBlocks).getOr(root);
  return CaretFinder.fromPosition(forward, scope.dom(), pos).filter(isBr);
};
Example #22
0
const getClosestBlock = (root: Element, pos: CaretPosition): Element => {
  const parentBlocks = Arr.filter(Parents.parentsAndSelf(Element.fromDom(pos.container()), root), ElementType.isBlock);
  return Arr.head(parentBlocks).getOr(root);
};
Example #23
0
 export const isBelow = (pos1: CaretPosition, pos2: CaretPosition): boolean => {
   return Options.liftN([Arr.last(pos2.getClientRects()), Arr.head(pos1.getClientRects())], GeomClientRect.isBelow).getOr(false);
 };
Example #24
0
const isAbove = (pos1: CaretPosition, pos2: CaretPosition): boolean => {
  return Options.liftN([Arr.head(pos2.getClientRects()), Arr.last(pos1.getClientRects())], ClientRect.isAbove).getOr(false);
};