(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); }
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); }); }); }); };
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); };
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); };
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); }); };
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; } })); };
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; } ); };
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(); } ); };
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(); } }); };
find: (comp: AlloyComponent) => { const children = Replacing.contents(comp); return Arr.head(children); }