Add core and utils

This commit is contained in:
denis
2018-12-02 19:40:39 +01:00
parent c2fac6d235
commit ee2293da4b
10 changed files with 2160 additions and 12 deletions
@@ -0,0 +1,328 @@
import * as utils from "ckeditor__ckeditor5-utils";
import * as core from "ckeditor__ckeditor5-core";
import * as engine from "ckeditor__ckeditor5-engine";
declare let bool: boolean;
declare let command: core.Command;
declare let commandCollection: core.CommandCollection;
declare let commands: core.Command[];
declare let componentFactory: core.ComponentFactory;
declare let config: utils.Config;
declare let conversion: engine.conversion.Conversion;
declare let dataController: engine.controller.DataController;
declare let editingController: engine.controller.EditingController;
declare let editingKeystrokeHandler: core.EditingKeystrokeHandler;
declare let editor: core.editor.Editor;
declare let focusTracker: utils.FocusTracker;
declare let htmlElement: HTMLElement;
declare let keystrokes: core.EditingKeystrokeHandler;
declare let locale: utils.Locale;
declare let model: engine.model.Model;
declare let num: number;
declare let pendingActions: core.PendingActions;
declare let plugin: core.Plugin;
declare let pluginCollection: core.PluginCollection<any>;
declare let pluginCollectionStr: core.PluginCollection<core.Plugin<string>>;
declare let pluginStr: core.Plugin<string>;
declare let myPlugin: MyPlugin;
declare let myPluginMaybe: MyPlugin | undefined;
declare let myPlugins: MyPlugin[];
declare let str: string;
declare let strOrUndef: string | undefined;
declare let strs: string[];
declare let ui: core.editor.EditorUI;
declare let undef: undefined;
declare let view: core.EditorUIView;
class MyPlugin extends core.Plugin {
constructor(editor: core.editor.Editor) {
super(editor);
}
static get pluginName() {
return "A";
}
myMethod() {
return "this is MyPlugin method";
}
}
class SomeCommand extends core.Command {
execute() {}
}
// core/editor/utils/dataapimixin =============================================
class DataApiEditor extends core.editor.Editor implements core.editor.utils.DataApi {
// mixin by DataApiMixin
getData: () => string;
setData: (data: string) => void;
}
utils.mix(DataApiEditor, core.editor.utils.DataApiMixin);
const dataApiEditor = new DataApiEditor();
dataApiEditor.setData("foo");
str = dataApiEditor.getData();
// core/editor/utils/elementapimixin ==========================================
class ElementApiEditor extends core.editor.Editor implements core.editor.utils.ElementApi {
// mixin by ElementApiMixin
readonly sourceElement: HTMLElement;
updateSourceElement: () => void;
}
utils.mix(ElementApiEditor, core.editor.utils.ElementApiMixin);
const elementApiEditor = new ElementApiEditor();
htmlElement = elementApiEditor.sourceElement;
elementApiEditor.updateSourceElement();
// core/editor/utils/attachtoform =============================================
// $ExpectError
core.editor.utils.attachToForm(editor);
core.editor.utils.attachToForm(elementApiEditor);
// core/editor/editor =========================================================
commandCollection = editor.commands;
config = editor.config;
editingController = editor.editing;
conversion = editor.conversion;
dataController = editor.data;
editor.isReadOnly = bool;
editingKeystrokeHandler = editor.keystrokes;
locale = editor.locale;
model = editor.model;
pluginCollection = editor.plugins;
if (editor.state === "initializing") {
console.log("new Editor()");
} else if (editor.state === "ready") {
console.log("Editor.create()");
} else if (editor.state === "destroyed") {
console.log("editor.destroy");
} else {
const n: never = editor.state;
}
editor.state = "ready";
core.editor.Editor.builtinPlugins = [plugin, pluginStr, plugin];
const plugins: Array<core.Plugin<any>> = core.editor.Editor.builtinPlugins;
core.editor.Editor.defaultConfig = {
foo: {
a: 1,
b: 2
}
};
editor = new core.editor.Editor();
editor = new core.editor.Editor({language: "pl"});
editor.destroy();
editor.destroy().then(() => {
console.log(`${editor.state} == destroyed`);
});
editor.execute("someCommand");
editor.execute("someCommand2", "arg1", 2);
editor.initPlugins();
editor.initPlugins().then(() => {
console.log(`init ${Array.from(editor.plugins).length} plugins`);
});
locale.t = editor.t;
str = editor.t("Label");
str = editor.t('Created file "%0" in %1ms.', ["fileName", "100"]);
// core/editor/editorui =======================================================
componentFactory = ui.componentFactory;
editor = ui.editor;
focusTracker = ui.focusTracker;
view = ui.view;
ui = new core.editor.EditorUI(editor, view);
ui.destroy();
ui.update();
// core/editor/editorwithui ===================================================
declare let ewui: core.editor.EditorWithUI;
htmlElement = ewui.element!;
ui = ewui.ui;
// core/command ===============================================================
editor = command.editor;
bool = command.isEnabled;
undef = command.value;
strOrUndef = new core.Command<string>(editor).value;
command = new core.Command(editor);
command.destroy();
command.execute();
command.refresh();
// core/commandcollection =====================================================
commandCollection = new core.CommandCollection();
const collectionsPairs = Array.from(commandCollection);
const collectionsPair = collectionsPairs[0];
str = collectionsPair[0];
command = collectionsPair[1];
commandCollection.add("foo", new SomeCommand(editor));
command = commandCollection.commands().next().value;
commands = Array.from(commandCollection.commands());
commandCollection.destroy();
commandCollection.execute("foo");
commandCollection.execute("bar", 1, "param");
command = commandCollection.get("foo");
str = commandCollection.names().next().value;
strs = Array.from(commandCollection.names());
// core/editingkeystrokehandler ===============================================
keystrokes = new core.EditingKeystrokeHandler(editor);
editor = keystrokes.editor;
keystrokes.press({keyCode: 123});
keystrokes.set("Ctrl+A", "foo");
keystrokes.set(["shift", "33"], "foo");
keystrokes.set(["ctrl", "A"], "foo", {priority: 10});
keystrokes.set(["ctrl", "A"], "foo", {priority: "high"});
keystrokes.set(["ctrl", "A"], () => console.log("key"));
keystrokes.set(["ctrl", "A"], (keyEvtData, cancel) => {
console.log(keyEvtData.keyCode);
cancel();
});
// core/pendingactions ========================================================
pendingActions = new core.PendingActions(editor);
str = core.PendingActions.pluginName;
const firstAction = pendingActions.first;
bool = pendingActions.hasAny;
strs = Array.from(pendingActions, action => action.message);
const action1 = pendingActions.add("Action 1");
pendingActions.remove(action1);
firstAction!.fire("I'm an Observable with a message");
str = firstAction!.message;
str = action1.message;
// core/plugin ================================================================
class MyPluginMini extends core.Plugin {
constructor(editor: core.editor.Editor) {
super(editor);
}
}
class MyPluginAll extends core.Plugin<string> {
constructor(editor: core.editor.Editor) {
super(editor);
}
static get pluginName() {
return "All";
}
static get requires() {
return [MyPlugin, MyPluginMini];
}
afterInit() {return Math.random() ? null : Promise.resolve("resolved"); }
destroy() {return Math.random() ? null : Promise.resolve("destroy"); }
init() {return Math.random() ? null : Promise.resolve("init"); }
}
plugin = new MyPlugin(editor);
plugin = new MyPluginMini(editor);
pluginStr = new MyPluginAll(editor);
const Plugin: MyPlugin = new MyPlugin(editor);
const pluginMini: MyPluginMini = new MyPluginMini(editor);
const pluginAll: MyPluginAll = new MyPluginAll(editor);
editor = myPlugin.editor;
str = MyPluginAll.pluginName;
strOrUndef = MyPluginMini.pluginName; // Todo: should be undefined.
plugin = new MyPluginAll.requires[0](editor);
const fs: Function[] = MyPluginAll.requires;
const fsOrUndef = MyPluginMini.requires; // Todo: should be undefined.
// $ExpectError
pluginMini.afterInit();
pluginAll.afterInit();
// $ExpectError
pluginMini.destroy();
pluginAll.destroy();
// $ExpectError
pluginMini.init();
pluginAll.init();
// core/plugincollection ======================================================
pluginCollectionStr = new core.PluginCollection(editor, [MyPluginAll]);
// $ExpectError
pluginCollectionStr = new core.PluginCollection(editor, [MyPlugin]);
const aColl = new core.PluginCollection<MyPluginAll | MyPluginMini>(editor, [MyPluginAll, MyPluginMini]);
const myColl = new core.PluginCollection(editor, [MyPlugin]);
const myCollArray = Array.from(myColl);
myPlugins = myCollArray.map(entry => entry[1]);
myPlugin = new (myCollArray[0][0])(editor);
myColl.destroy()
.then(destroyedPlugins => {
num = destroyedPlugins.length;
const plugin = destroyedPlugins[0];
editor = plugin.editor;
plugin.init!();
plugin.destroy();
str = plugin.myMethod();
});
myPluginMaybe = myColl.get("A");
myPluginMaybe = myColl.get(MyPlugin);
myColl.load([MyPlugin, "A"])
.then((loadedPlugins) => {
const plugin = loadedPlugins[0];
editor = plugin.editor;
str = plugin.myMethod();
});
myColl.load([MyPlugin, "A"], [MyPlugin, "A"]);
+269
View File
@@ -0,0 +1,269 @@
// Type definitions for @ckeditor/ckeditor5-core 11.0
// Project: https://github.com/ckeditor/ckeditor5-core
// Definitions by: denisname <https://github.com/denisname>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import * as engine from "ckeditor__ckeditor5-engine";
import * as ckutils from "ckeditor__ckeditor5-utils";
// TODO: depends on other libraries
export interface AlignmentConfig {}
export interface AutosaveConfig {}
export interface CKFinderAdapterConfig {}
export interface CloudServicesConfig {}
export interface FontFamilyConfig {}
export interface FontSizeConfig {}
export interface HeadingConfig {}
export interface HighlightConfig {}
export interface ImageConfig {}
export interface MediaEmbedConfig {}
export interface TypingConfig {}
export interface ComponentFactory {} // CKEditor 5 UI
export interface EditorUIView {} // CKEditor 5 UI
export namespace editor {
namespace utils {
// core/editor/utils/attachtoform
function attachToForm(editor: Editor & ElementApi): void;
// core/editor/utils/dataapimixin
const DataApiMixin: DataApi;
interface DataApi {
getData(): string;
setData(data: string): void;
}
// core/editor/utils/elementapimixin
const ElementApiMixin: ElementApi;
interface ElementApi {
readonly sourceElement: HTMLElement;
updateSourceElement(): void;
}
}
// core/editor/editor
class Editor implements ckutils.Emitter, ckutils.Observable {
readonly commands: CommandCollection;
readonly config: ckutils.Config;
readonly conversion: engine.conversion.Conversion;
readonly data: engine.controller.DataController;
readonly editing: engine.controller.EditingController;
isReadOnly: boolean;
readonly keystrokes: EditingKeystrokeHandler;
readonly locale: ckutils.Locale;
readonly model: engine.model.Model;
readonly plugins: PluginCollection<Plugin<any>>;
state: "initializing" | "ready" | "destroyed";
static builtinPlugins: Array<Plugin<any>>;
static defaultConfig: object;
constructor(config?: object);
destroy(): Promise<void>;
execute(commandName: string, ...commandParams: any[]): void;
initPlugins(): Promise<void>;
t(str: string, values?: string[]): string;
static create(config: object): Promise<any>;
// Emitter
delegate(...events: string[]): ckutils.EmitterMixinDelegateChain;
fire(eventOrInfo: string | ckutils.EventInfo<ckutils.Emitter>, ...args: any[]): any;
listenTo(emitter: ckutils.Emitter, event: string, callback: Function, options?: {priority?: ckutils.PriorityString | number }): void;
off(event: string, callback?: Function): void;
on(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
once(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
stopDelegating(event?: string, emitter?: ckutils.Emitter): void;
stopListening(emitter?: ckutils.Emitter, event?: string, callback?: Function): void;
// Observable
bind(...bindProperties: string[]): ckutils.BindChain;
decorate(methodName: string): void;
set(name: object): void;
set(name: string, value: any): void;
unbind(...unbindProperties: string[]): void;
}
// core/editor/editorconfig
interface EditorConfig {
alignment: AlignmentConfig;
autosave: AutosaveConfig;
balloonToolbar: string[];
blockToolbar: string[];
ckfinder: CKFinderAdapterConfig;
cloudServices: CloudServicesConfig;
fontFamily: FontFamilyConfig;
fontSize: FontSizeConfig;
heading: HeadingConfig;
highlight: HighlightConfig;
image: ImageConfig;
language: string;
mediaEmbed: MediaEmbedConfig;
plugins: Array<string | Plugin>;
removePlugins: string[];
toolbar: string[] | {items: string[]; viewportTopOffset: number; };
typing: TypingConfig;
}
// core/editor/editorui
class EditorUI implements ckutils.Emitter {
readonly componentFactory: ComponentFactory;
readonly editor: Editor;
readonly focusTracker: ckutils.FocusTracker;
readonly view: EditorUIView;
constructor(editor: Editor, view: EditorUIView);
destroy(): void;
update(): void;
// Emitter
delegate(...events: string[]): ckutils.EmitterMixinDelegateChain;
fire(eventOrInfo: string | ckutils.EventInfo<ckutils.Emitter>, ...args: any[]): any;
listenTo(emitter: ckutils.Emitter, event: string, callback: Function, options?: {priority?: ckutils.PriorityString | number }): void;
off(event: string, callback?: Function): void;
on(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
once(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
stopDelegating(event?: string, emitter?: ckutils.Emitter): void;
stopListening(emitter?: ckutils.Emitter, event?: string, callback?: Function): void;
}
// core/editor/editorwithui
interface EditorWithUI {
readonly element: HTMLElement | null;
readonly ui: EditorUI;
}
}
// core/command
export class Command<T = undefined> implements ckutils.Emitter, ckutils.Observable {
readonly editor: editor.Editor;
readonly isEnabled: boolean;
readonly value: T | undefined;
constructor(editor: editor.Editor);
destroy(): void;
execute(): void;
refresh(): void;
// Emitter
delegate(...events: string[]): ckutils.EmitterMixinDelegateChain;
fire(eventOrInfo: string | ckutils.EventInfo<ckutils.Emitter>, ...args: any[]): any;
listenTo(emitter: ckutils.Emitter, event: string, callback: Function, options?: {priority?: ckutils.PriorityString | number }): void;
off(event: string, callback?: Function): void;
on(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
once(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
stopDelegating(event?: string, emitter?: ckutils.Emitter): void;
stopListening(emitter?: ckutils.Emitter, event?: string, callback?: Function): void;
// Observable
bind(...bindProperties: string[]): ckutils.BindChain;
decorate(methodName: string): void;
set(name: object): void;
set(name: string, value: any): void;
unbind(...unbindProperties: string[]): void;
}
// core/commandcollection
export class CommandCollection {
constructor();
[Symbol.iterator](): Iterator<[string, Command]>;
add(commandName: string, command: Command): void;
commands(): IterableIterator<Command>;
destroy(): void;
execute(commandName: string, ...commandParams: any[]): void;
get(commandName: string): Command;
names(): IterableIterator<string>;
}
// core/editingkeystrokehandler
export class EditingKeystrokeHandler extends ckutils.KeystrokeHandler {
readonly editor: editor.Editor;
constructor(editor: editor.Editor);
set(
keystroke: string | Array<string | number>,
callback: string | ((keyEvtData: engine.view.observer.KeyEventData, cancel: () => void) => void),
options?: {priority: ckutils.PriorityString | number}
): void;
}
// core/pendingactions
export class PendingActions extends Plugin {
static readonly pluginName: "PendingActions";
first: null | ckutils.Observable & {message: string};
readonly hasAny: boolean;
[Symbol.iterator](): Iterator<ckutils.Observable & {message: string}>;
add(message: string): ckutils.Observable & {message: string};
remove(action: ckutils.Observable & {message: string}): void;
}
// core/plugin
export abstract class Plugin<T = void> implements ckutils.Emitter, ckutils.Observable {
readonly editor: editor.Editor;
static readonly pluginName?: string;
static readonly requires?: Array<new(editor: editor.Editor) => Plugin>;
constructor(editor: editor.Editor);
afterInit?(): null | Promise<T>;
destroy?(): null | Promise<T>;
init?(): null | Promise<T>;
// Emitter
delegate(...events: string[]): ckutils.EmitterMixinDelegateChain;
fire(eventOrInfo: string | ckutils.EventInfo<ckutils.Emitter>, ...args: any[]): any;
listenTo(emitter: ckutils.Emitter, event: string, callback: Function, options?: {priority?: ckutils.PriorityString | number }): void;
off(event: string, callback?: Function): void;
on(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
once(event: string, callback: Function, options?: {priority: ckutils.PriorityString | number}): void;
stopDelegating(event?: string, emitter?: ckutils.Emitter): void;
stopListening(emitter?: ckutils.Emitter, event?: string, callback?: Function): void;
// Observable
bind(...bindProperties: string[]): ckutils.BindChain;
decorate(methodName: string): void;
set(name: object): void;
set(name: string, value: any): void;
unbind(...unbindProperties: string[]): void;
}
// PluginInterface, see Plugin
// core/plugincollection
export class PluginCollection<P extends Plugin<any>> {
constructor(
editor: editor.Editor,
availablePlugins?: Array<new(editor: editor.Editor) => P>
);
[Symbol.iterator](): Iterator<[new(editor: editor.Editor) => P, P]>;
destroy(): Promise<Array<P & {destroy(): void | null | Promise<any>}>>;
get(
key: string | (new(editor: editor.Editor) => P)
): P | undefined;
load(
plugins: Array<string | (new(editor: editor.Editor) => P)>,
removePlugins?: Array<string | (new(editor: editor.Editor) => P)>
): Promise<P[]>;
}
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": [
"es6",
"dom",
"es2015.promise"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"ckeditor__ckeditor5-core-tests.ts"
]
}
@@ -0,0 +1,22 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": {
"options": [
["Object", "Avoid using the `Object` type. Did you mean `object`?"],
["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"],
["Number", "Avoid using the `Number` type. Did you mean `number`?"],
["String", "Avoid using the `String` type. Did you mean `string`?"],
["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"]
]
},
"no-empty-interface": false,
"prefer-switch": false,
"quotemark": [
true,
"double",
"avoid-escape",
"avoid-template"
]
}
}
@@ -0,0 +1,81 @@
import * as engine from "ckeditor__ckeditor5-engine";
declare let pattern: engine.view.MatcherPattern;
pattern = {name: /^p/};
pattern = {
attributes: {
title: "foobar",
foo: /^\w+/,
bar: true,
}
};
pattern = {
classes: "foobar"
};
pattern = {
classes: /foo.../
};
pattern = {
classes: ["baz", "bar", /foo.../]
};
pattern = {
styles: {
position: "absolute",
color: /^\w*blue$/
}
};
pattern = {
name: "span",
styles: {
"font-weight": "bold"
},
classes: "highlighted"
};
pattern = (element: engine.view.Element) => {
if (element.name === "div" && element.childCount > 0) {
return {name: true};
}
return null;
};
pattern = (element: engine.view.Element) => {
if (element.name === "p") {
const fontSize = element.getStyle("font-size")!;
const size = fontSize.match(/(\d+)/px);
if (size && Number(size[1]) > 26) {
return {name: true, attribute: ["font-size"]};
}
}
return null;
};
declare let viewDefinition: engine.view.ElementDefinition;
viewDefinition = "p";
viewDefinition = {
name: "h1",
classes: ["foo", "bar"],
};
viewDefinition = {
name: "span",
styles: {
"font-size": "12px",
"font-weight": "bold",
},
attributes: {
"data-id": "123",
}
};
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"ckeditor__ckeditor5-engine-tests.ts"
]
}
@@ -0,0 +1,20 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": {
"options": [
["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"],
["Number", "Avoid using the `Number` type. Did you mean `number`?"],
["String", "Avoid using the `String` type. Did you mean `string`?"],
["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"]
]
},
"no-empty-interface": false,
"quotemark": [
true,
"double",
"avoid-escape",
"avoid-template"
]
}
}
@@ -489,24 +489,30 @@ utils.mapsEqual(map, map);
// utils/mix ==================================================================
class Editor {
b: () => number;
}
interface SomeMixin {
a: () => string;
}
class Editor implements SomeMixin {
a: () => string;
b() { return 3; }
}
const SomeMixin = {
a() {
return "a";
}
a() { return "a"; }
};
const SomeMixinNum = {
a() { return 3; }
};
utils.mix(Editor, SomeMixin);
const mixEditor = new Editor() as Editor & SomeMixin;
mixEditor.a();
mixEditor.b();
// $ExpectError
utils.mix(Editor, SomeMixinNum);
const editor = new Editor();
str = editor.a();
num = editor.b();
// utils/nth ==================================================================
+6 -2
View File
@@ -367,7 +367,11 @@ export class KeystrokeHandler {
destroy(): void;
listenTo(emitter: Emitter): void;
press(keyEvtData: KeystrokeInfo): boolean;
set(keystroke: string | Array<string | number>, callback: Function, options?: {priority?: PriorityString | number}): void;
set(
keystroke: string | Array<string | number>,
callback: (keyEvtData: KeystrokeInfo, cancel: () => void) => void,
options?: {priority?: PriorityString | number}
): void;
}
// utils/locale
@@ -392,7 +396,7 @@ export function mapsEqual<K, V>(mapsA: Map<K, V>, mapsB: Map<K, V>): boolean;
// utils/mix
export function mix(baseClass: {new(): any}, ...mixins: any[]): void;
export function mix<T>(baseClass: {new(...p: any[]): T}, ...mixins: Array<Partial<T>>): void;
// utils/nth