import { Chain, Cursors, StructAssert } from '@ephox/agar'; import { Editor } from '../../alien/EditorTypes'; import * as Options from '../../alien/Options'; import * as TinyAssertions from '../bdd/TinyAssertions'; import * as TinySelections from '../bdd/TinySelections'; import { Presence } from './TinyApis'; export interface ApiChains { cNodeChanged: () => Chain; cSetContent: (html: string) => Chain; cSetRawContent: (html: string) => Chain; cSetSelectionFrom: (spec: Cursors.CursorSpec | Cursors.RangeSpec) => Chain; cSetSelection: (startPath: number[], soffset: number, finishPath: number[], foffset: number) => Chain; cSetCursor: (elementPath: number[], offset: number) => Chain; /** @deprecated use cSetOption instead */ cDeleteSetting: (key: string) => Chain; cUnsetOption: (key: string) => Chain; /** @deprecated use cSetOption instead */ cSetSetting: (key: string, value: any) => Chain; cSetOption: (key: string, value: any) => Chain; cGetContent: Chain; cExecCommand: (command: string, value?: any) => Chain; cAssertContent: (expected: string) => Chain; cAssertContentPresence: (expected: Presence) => Chain; cAssertContentStructure: (expected: StructAssert) => Chain; cSelect: (selector: string, path: number[]) => Chain; cFocus: Chain; cAssertSelection: (startPath: number[], soffset: number, finishPath: number[], foffset: number) => Chain; } const cNodeChanged = (): Chain => Chain.op((editor: T) => { editor.nodeChanged(); }); const cSetContent = (html: string): Chain => { return Chain.op((editor: T) => { editor.setContent(html); }); }; const cSetRawContent = (html: string): Chain => { return Chain.op((editor: T) => { editor.getBody().innerHTML = html; }); }; const cSetSelectionFrom = (spec: Cursors.CursorSpec | Cursors.RangeSpec): Chain => { const path = Cursors.pathFrom(spec); return cSetSelection(path.startPath, path.soffset, path.finishPath, path.foffset); }; const cSetCursor = (elementPath: number[], offset: number): Chain => { return cSetSelection(elementPath, offset, elementPath, offset); }; const cSetSelection = (startPath: number[], soffset: number, finishPath: number[], foffset: number): Chain => { return Chain.op((editor: T) => { TinySelections.setSelection(editor, startPath, soffset, finishPath, foffset); }); }; const cSetOption = (key: string, value: any): Chain => { return Chain.op((editor: T) => { Options.set(editor, key, value); }); }; const cUnsetOption = (key: string): Chain => { return Chain.op((editor: T) => { Options.unset(editor, key); }); }; const cSelect = (selector: string, path: number[]): Chain => { return Chain.op((editor: T) => { TinySelections.select(editor, selector, path); }); }; const cGetContent: Chain = Chain.mapper((editor: Editor) => { return editor.getContent(); }); const cExecCommand = (command: string, value?: any): Chain => { return Chain.op((editor: T) => { editor.execCommand(command, false, value); }); }; const cAssertContent = (expected: string): Chain => { return Chain.op((editor: T) => { TinyAssertions.assertContent(editor, expected); }); }; const cAssertContentPresence = (expected: Presence): Chain => { return Chain.op((editor: T) => { TinyAssertions.assertContentPresence(editor, expected); }); }; const cAssertContentStructure = (expected: StructAssert): Chain => { return Chain.op((editor: T) => { TinyAssertions.assertContentStructure(editor, expected); }); }; const cAssertSelection = (startPath: number[], soffset: number, finishPath: number[], foffset: number): Chain => { return Chain.op((editor: T) => { TinyAssertions.assertSelection(editor, startPath, soffset, finishPath, foffset); }); }; const cFocus = Chain.op((editor: Editor) => { editor.focus(); }); export const ApiChains: ApiChains = { cSetContent, cGetContent, cSetSelectionFrom, cSetSelection, cSetSetting: cSetOption, cSetOption, cSetRawContent, cDeleteSetting: cUnsetOption, cUnsetOption, cSetCursor, cSelect, cExecCommand, cNodeChanged, cFocus, cAssertContent, cAssertContentPresence, cAssertContentStructure, cAssertSelection };