@textbus/platform-browser 3.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,123 @@
1
+ Textbus PC 浏览器支持模块
2
+ =====================
3
+
4
+ Textbus 是一套用于构建富交互的富文本编辑框架。和大多数富文本编辑器不同的是,Textbus 以组件为核心,格式为辅助,并大幅简化了富文本编辑器开发中常见
5
+ API,且提供了更高的抽象层,使 Textbus 不仅易于上手,同时还能驱动复杂的富文本应用。
6
+
7
+ 本项目为 Textbus PC 端浏览器中间层实现,提供了 Textbus PC 端所需要的编辑器基本支持能力。如光标、选区桥接、DOM 解析及渲染能力桥接等。
8
+
9
+ 如果你需要一个开箱即用的编辑器,请参考[官方文档](https://textbus.io)。
10
+
11
+ ## 安装
12
+
13
+ ```
14
+ npm install @textbus/core @textbus/browser
15
+ ```
16
+
17
+ ## 创建编辑器
18
+
19
+ ```ts
20
+ import { Viewer, ViewOptions } from '@textbus/browser'
21
+
22
+ import { rootComponent, rootComponentLoader } from './root.component'
23
+
24
+ const config: ViewOptions = {
25
+ // ...配置项
26
+ }
27
+
28
+ const editor = new Viewer(rootComponent, rootComponentLoader, config)
29
+ ```
30
+
31
+ 其中 `rootComponent`,`rootComponentLoader` 实现请参考[官方文档](https://textbus.io/docs/guide)。
32
+
33
+ ### 配置项
34
+
35
+ 配置项接口如下:
36
+
37
+ ```ts
38
+ export interface ViewModule extends Module {
39
+ componentLoaders?: ComponentLoader[]
40
+ formatLoaders?: FormatLoader[]
41
+ }
42
+
43
+ /**
44
+ * Textbus PC 端配置接口
45
+ */
46
+ export interface ViewOptions extends TextbusConfig {
47
+ imports?: ViewModule[]
48
+ /** 自动获取焦点 */
49
+ autoFocus?: boolean
50
+ /** 编辑区最小高度 */
51
+ minHeight?: string
52
+ /** 组件加载器 */
53
+ componentLoaders?: ComponentLoader[]
54
+ /** 格式加载器 */
55
+ formatLoaders?: FormatLoader[]
56
+ /** 默认内容 */
57
+ content?: string | ComponentLiteral
58
+ /** 文档默认样式表 */
59
+ styleSheets?: string[]
60
+ /** 配置文档编辑状态下用到的样式 */
61
+ editingStyleSheets?: string[]
62
+ }
63
+ ```
64
+
65
+ ### 启动
66
+
67
+ ```ts
68
+ const host = document.getElementById('editor')
69
+
70
+ editor.mount(host).then(() => {
71
+ console.log('编辑器创建成功!')
72
+ })
73
+ ```
74
+
75
+ ### 销毁编辑器
76
+
77
+ ```ts
78
+ editor.destroy()
79
+ ```
80
+
81
+ ### 获取焦点
82
+
83
+ ```ts
84
+ editor.focus()
85
+ ```
86
+
87
+ ### 取消焦点
88
+
89
+ ```ts
90
+ editor.blur()
91
+ ```
92
+
93
+ ### 获取 HTML 内容
94
+
95
+ ```ts
96
+ const content = editor.getContents().content
97
+ ```
98
+
99
+ ### 获取 JSON 内容
100
+
101
+ ```ts
102
+ const json = editor.getJSON().content
103
+ ```
104
+
105
+ ### 替换内容
106
+
107
+ ```ts
108
+ editor.replaceContent('<p>新内容!</p>')
109
+
110
+ editor.replaceContent({
111
+ // 必须为 Textbus 导出的 JSON 格式
112
+ })
113
+ ```
114
+
115
+ ### 清空编辑器
116
+
117
+ ```ts
118
+ editor.replaceContent('')
119
+ ```
120
+
121
+ ### 官方文档
122
+
123
+ 更多文档请参考:[中文文档](https://textbus.io)
@@ -0,0 +1,3 @@
1
+ export declare const isWindows: () => boolean;
2
+ export declare const isMac: () => boolean;
3
+ export declare const isSafari: () => boolean;
@@ -0,0 +1,16 @@
1
+ export interface UIElementParams {
2
+ classes?: string[];
3
+ attrs?: {
4
+ [key: string]: any;
5
+ };
6
+ props?: {
7
+ [key: string]: any;
8
+ };
9
+ styles?: {
10
+ [key: string]: any;
11
+ };
12
+ children?: (Node | null)[];
13
+ on?: Record<string, (ev: Event) => void>;
14
+ }
15
+ export declare function createElement(tagName: string, options?: UIElementParams): HTMLElement;
16
+ export declare function createTextNode(content: string): Text;
@@ -0,0 +1,44 @@
1
+ import { Injector } from '@tanbo/di';
2
+ import { Selection, SelectionPaths, AbstractSelection, Scheduler } from '@textbus/core';
3
+ import { SelectionBridge, Rect } from '../core/selection-bridge';
4
+ export interface RemoteSelection {
5
+ id: string;
6
+ color: string;
7
+ username: string;
8
+ paths: SelectionPaths;
9
+ }
10
+ export interface SelectionRect extends Rect {
11
+ color: string;
12
+ username: string;
13
+ id: string;
14
+ }
15
+ export interface RemoteSelectionCursor {
16
+ cursor: HTMLElement;
17
+ anchor: HTMLElement;
18
+ userTip: HTMLElement;
19
+ }
20
+ export declare abstract class CollaborateSelectionAwarenessDelegate {
21
+ abstract getRects(abstractSelection: AbstractSelection, nativeRange: Range): false | Rect[];
22
+ }
23
+ export declare class CollaborateCursor {
24
+ private injector;
25
+ private nativeSelection;
26
+ private scheduler;
27
+ private selection;
28
+ private awarenessDelegate;
29
+ private host;
30
+ private canvasContainer;
31
+ private canvas;
32
+ private context;
33
+ private tooltips;
34
+ private onRectsChange;
35
+ private subscription;
36
+ private currentSelection;
37
+ private container;
38
+ constructor(injector: Injector, nativeSelection: SelectionBridge, scheduler: Scheduler, selection: Selection, awarenessDelegate?: CollaborateSelectionAwarenessDelegate | null);
39
+ refresh(): void;
40
+ destroy(): void;
41
+ draw(paths: RemoteSelection[]): void;
42
+ protected drawUserCursor(rects: SelectionRect[]): void;
43
+ private getUserCursor;
44
+ }
@@ -0,0 +1,6 @@
1
+ export * from './caret';
2
+ export * from './dom-renderer';
3
+ export * from './injection-tokens';
4
+ export * from './input';
5
+ export * from './selection-bridge';
6
+ export * from './types';
@@ -0,0 +1,51 @@
1
+ import { Observable } from '@tanbo/stream';
2
+ import { Injector } from '@tanbo/di';
3
+ import { Scheduler } from '@textbus/core';
4
+ import { Rect } from './selection-bridge';
5
+ export declare function getLayoutRectByRange(range: Range): Rect;
6
+ export interface CaretPosition {
7
+ left: number;
8
+ top: number;
9
+ height: number;
10
+ }
11
+ export interface CaretStyle {
12
+ height: string;
13
+ lineHeight: string;
14
+ fontSize: string;
15
+ }
16
+ export interface CaretLimit {
17
+ top: number;
18
+ bottom: number;
19
+ }
20
+ export interface Scroller {
21
+ onScroll: Observable<any>;
22
+ getLimit(): CaretLimit;
23
+ setOffset(offsetScrollTop: number): void;
24
+ }
25
+ export declare class Caret {
26
+ private scheduler;
27
+ private injector;
28
+ onPositionChange: Observable<CaretPosition | null>;
29
+ onStyleChange: Observable<CaretStyle>;
30
+ elementRef: HTMLElement;
31
+ private timer;
32
+ private caret;
33
+ private oldPosition;
34
+ private set display(value);
35
+ private get display();
36
+ private _display;
37
+ private flashing;
38
+ private subs;
39
+ private positionChangeEvent;
40
+ private styleChangeEvent;
41
+ private oldRange;
42
+ private isFixed;
43
+ private editorMask;
44
+ constructor(scheduler: Scheduler, injector: Injector);
45
+ refresh(isFixedCaret?: boolean): void;
46
+ show(range: Range, restart: boolean): void;
47
+ hide(): void;
48
+ destroy(): void;
49
+ correctScrollTop(scroller: Scroller): void;
50
+ private updateCursorPosition;
51
+ }
@@ -0,0 +1,51 @@
1
+ import { NativeNode, NativeRenderer } from '@textbus/core';
2
+ /**
3
+ * Textbus PC 端浏览器渲染能力实现
4
+ */
5
+ export declare class DomRenderer implements NativeRenderer {
6
+ isSVG: RegExp;
7
+ xlinkNameSpace: string;
8
+ possibleXlinkNames: {
9
+ xlinkActuate: string;
10
+ xlinkactuate: string;
11
+ 'xlink:actuate': string;
12
+ xlinkArcrole: string;
13
+ xlinkarcrole: string;
14
+ 'xlink:arcrole': string;
15
+ xlinkHref: string;
16
+ xlinkhref: string;
17
+ 'xlink:href': string;
18
+ xlinkRole: string;
19
+ xlinkrole: string;
20
+ 'xlink:role': string;
21
+ xlinkShow: string;
22
+ xlinkshow: string;
23
+ 'xlink:show': string;
24
+ xlinkTitle: string;
25
+ xlinktitle: string;
26
+ 'xlink:title': string;
27
+ xlinkType: string;
28
+ xlinktype: string;
29
+ 'xlink:type': string;
30
+ };
31
+ formElement: Record<string, string[]>;
32
+ listen<T = any>(node: NativeNode, type: string, callback: (ev: T) => any): void;
33
+ unListen(node: NativeNode, type: string, callback: (ev: any) => any): void;
34
+ createTextNode(textContent: string): NativeNode;
35
+ createElement(name: string): NativeNode;
36
+ appendChild(parent: NativeNode, newChild: NativeNode): void;
37
+ remove(node: NativeNode): void;
38
+ insertBefore(newNode: NativeNode, ref: NativeNode): void;
39
+ getChildByIndex(parent: NativeNode, index: number): NativeNode | null;
40
+ addClass(target: NativeNode, name: string): void;
41
+ removeClass(target: NativeNode, name: string): void;
42
+ setStyle(target: NativeNode, key: string, value: any): void;
43
+ removeStyle(target: NativeNode, key: string): void;
44
+ setAttribute(target: NativeNode, key: string, value: string): void;
45
+ removeAttribute(target: NativeNode, key: string): void;
46
+ setXlinkAttribute(target: NativeNode, key: string, value: string): void;
47
+ removeXlinkAttribute(target: NativeNode, key: string): void;
48
+ replace(newChild: NativeNode, oldChild: NativeNode): void;
49
+ copy(): void;
50
+ static replaceEmpty(s: string, target: string): string;
51
+ }
@@ -0,0 +1,18 @@
1
+ import { InjectionToken } from '@tanbo/di';
2
+ import { ViewOptions } from './types';
3
+ /**
4
+ * 编辑器可选项依赖注入 token
5
+ */
6
+ export declare const EDITOR_OPTIONS: InjectionToken<ViewOptions>;
7
+ /**
8
+ * 编辑器容器依赖注入 token
9
+ */
10
+ export declare const VIEW_CONTAINER: InjectionToken<HTMLElement>;
11
+ /**
12
+ * 编辑器容器依赖注入 token
13
+ */
14
+ export declare const VIEW_DOCUMENT: InjectionToken<HTMLElement>;
15
+ /**
16
+ * 编辑器容器遮罩层 token
17
+ */
18
+ export declare const VIEW_MASK: InjectionToken<HTMLElement>;
@@ -0,0 +1,40 @@
1
+ import { Commander, Controller, Keyboard, Scheduler, Selection } from '@textbus/core';
2
+ import { Parser } from '../dom-support/parser';
3
+ import { Caret } from './caret';
4
+ /**
5
+ * Textbus PC 端输入实现
6
+ */
7
+ export declare class Input {
8
+ private parser;
9
+ private keyboard;
10
+ private commander;
11
+ private selection;
12
+ private controller;
13
+ private scheduler;
14
+ private caret;
15
+ static openExperimentalCompositionInput: boolean;
16
+ onReady: Promise<void>;
17
+ private container;
18
+ private subscription;
19
+ private doc;
20
+ private textarea;
21
+ private isFocus;
22
+ private inputFormatterId;
23
+ private inputFormatter;
24
+ private nativeFocus;
25
+ private isSafari;
26
+ private isMac;
27
+ private isWindows;
28
+ private isSougouPinYin;
29
+ constructor(parser: Parser, keyboard: Keyboard, commander: Commander, selection: Selection, controller: Controller, scheduler: Scheduler, caret: Caret);
30
+ focus(): void;
31
+ blur(): void;
32
+ destroy(): void;
33
+ private init;
34
+ private handleDefaultActions;
35
+ private handlePaste;
36
+ private handleShortcut;
37
+ private handleInput;
38
+ private experimentalCompositionInput;
39
+ private createEditableFrame;
40
+ }
@@ -0,0 +1,77 @@
1
+ import { Observable } from '@tanbo/stream';
2
+ import { Injector } from '@tanbo/di';
3
+ import { NativeSelectionBridge, NativeSelectionConnector, Renderer, SelectionPosition, AbstractSelection, RootComponentRef, Controller, Selection } from '@textbus/core';
4
+ import { Caret } from './caret';
5
+ import { Input } from './input';
6
+ /**
7
+ * 选区焦点可视位置
8
+ */
9
+ export interface Rect {
10
+ left: number;
11
+ top: number;
12
+ width: number;
13
+ height: number;
14
+ }
15
+ /**
16
+ * Textbus PC 端选区桥接实现
17
+ */
18
+ export declare class SelectionBridge implements NativeSelectionBridge {
19
+ private injector;
20
+ caret: Caret;
21
+ private controller;
22
+ private selection;
23
+ private rootComponentRef;
24
+ private input;
25
+ private renderer;
26
+ onSelectionChange: Observable<Range | null>;
27
+ nativeSelection: globalThis.Selection;
28
+ private selectionMaskElement;
29
+ private selectionChangeEvent;
30
+ private subs;
31
+ private sub;
32
+ private connector;
33
+ private ignoreSelectionChange;
34
+ private changeFromUser;
35
+ private docContainer;
36
+ private maskContainer;
37
+ private cacheCaretPositionTimer;
38
+ private oldCaretPosition;
39
+ constructor(injector: Injector, caret: Caret, controller: Controller, selection: Selection, rootComponentRef: RootComponentRef, input: Input, renderer: Renderer);
40
+ connect(connector: NativeSelectionConnector): void;
41
+ disConnect(): void;
42
+ getRect(location: SelectionPosition): Rect | null;
43
+ restore(abstractSelection: AbstractSelection | null, formLocal: boolean): void;
44
+ destroy(): void;
45
+ getPositionByRange(abstractSelection: AbstractSelection): {
46
+ focus: {
47
+ node: Node;
48
+ offset: number;
49
+ } | null;
50
+ anchor: {
51
+ node: Node;
52
+ offset: number;
53
+ } | null;
54
+ };
55
+ getPreviousLinePositionByCurrent(position: SelectionPosition): SelectionPosition | null;
56
+ getNextLinePositionByCurrent(position: SelectionPosition): SelectionPosition | null;
57
+ private getLinePosition;
58
+ /**
59
+ * 获取选区向上移动一行的位置。
60
+ * @param currentPosition
61
+ * @param startLeft 参考位置。
62
+ */
63
+ private getPreviousLinePositionByOffset;
64
+ /**
65
+ * 获取选区向下移动一行的位置。
66
+ * @param currentPosition
67
+ * @param startLeft 参考位置。
68
+ */
69
+ private getNextLinePositionByOffset;
70
+ private unListen;
71
+ private listen;
72
+ private syncSelection;
73
+ private findSelectedNodeAndOffset;
74
+ private getCorrectedPosition;
75
+ private findFocusNode;
76
+ private findFocusNodeByParent;
77
+ }
@@ -0,0 +1,29 @@
1
+ import { ComponentLiteral, Module, TextbusConfig } from '@textbus/core';
2
+ import { FormatLoader, ComponentLoader, AttributeLoader } from '../dom-support/parser';
3
+ export interface ViewModule extends Module {
4
+ componentLoaders?: ComponentLoader[];
5
+ formatLoaders?: FormatLoader<any>[];
6
+ attributeLoaders?: AttributeLoader<any>[];
7
+ }
8
+ /**
9
+ * Textbus PC 端配置接口
10
+ */
11
+ export interface ViewOptions extends TextbusConfig {
12
+ imports?: ViewModule[];
13
+ /** 自动获取焦点 */
14
+ autoFocus?: boolean;
15
+ /** 编辑区最小高度 */
16
+ minHeight?: string;
17
+ /** 组件加载器 */
18
+ componentLoaders?: ComponentLoader[];
19
+ /** 格式加载器 */
20
+ formatLoaders?: FormatLoader<any>[];
21
+ /** 属性加载器 */
22
+ attributeLoaders?: AttributeLoader<any>[];
23
+ /** 默认内容 */
24
+ content?: string | ComponentLiteral;
25
+ /** 文档默认样式表 */
26
+ styleSheets?: string[];
27
+ /** 配置文档编辑状态下用到的样式 */
28
+ editingStyleSheets?: string[];
29
+ }
@@ -0,0 +1,2 @@
1
+ export * from './output-translator';
2
+ export * from './parser';
@@ -0,0 +1,16 @@
1
+ import { VElement } from '@textbus/core';
2
+ /**
3
+ * HTML 输出转换器
4
+ */
5
+ export declare class OutputTranslator {
6
+ static singleTags: string[];
7
+ static simpleXSSFilter: {
8
+ text(text: string): string;
9
+ attrName(text: string): string;
10
+ attrValue(text: string): string;
11
+ };
12
+ private singleTagTest;
13
+ transform(vDom: VElement): string;
14
+ private vDomToHTMLString;
15
+ private replaceEmpty;
16
+ }
@@ -0,0 +1,56 @@
1
+ import { Injector } from '@tanbo/di';
2
+ import { Attribute, ComponentInstance, Formatter, FormatValue, Slot } from '@textbus/core';
3
+ import { ViewOptions } from '../core/types';
4
+ export interface ComponentResources {
5
+ links?: Array<{
6
+ [key: string]: string;
7
+ }>;
8
+ styles?: string[];
9
+ scripts?: string[];
10
+ editModeStyles?: string[];
11
+ }
12
+ export interface SlotParser {
13
+ <T extends Slot>(childSlot: T, slotRootElement: HTMLElement, slotContentHostElement?: HTMLElement): T;
14
+ }
15
+ /**
16
+ * 组件加载器
17
+ */
18
+ export interface ComponentLoader {
19
+ /** 组件所需要的外部资源 */
20
+ resources?: ComponentResources;
21
+ /** 识别组件的匹配方法 */
22
+ match(element: HTMLElement): boolean;
23
+ /** 读取组件内容的方法 */
24
+ read(element: HTMLElement, context: Injector, slotParser: SlotParser): ComponentInstance | Slot;
25
+ }
26
+ export interface FormatLoaderReadResult<T extends FormatValue> {
27
+ formatter: Formatter<T>;
28
+ value: T;
29
+ }
30
+ export interface FormatLoader<T extends FormatValue> {
31
+ match(element: HTMLElement): boolean;
32
+ read(element: HTMLElement): FormatLoaderReadResult<T>;
33
+ }
34
+ export interface AttributeLoaderReadResult<T extends FormatValue> {
35
+ attribute: Attribute<T>;
36
+ value: T;
37
+ }
38
+ export interface AttributeLoader<T extends FormatValue> {
39
+ match(element: HTMLElement): boolean;
40
+ read(element: HTMLElement): AttributeLoaderReadResult<T>;
41
+ }
42
+ export declare class Parser {
43
+ private options;
44
+ private injector;
45
+ static parseHTML(html: string): HTMLElement;
46
+ componentLoaders: ComponentLoader[];
47
+ formatLoaders: FormatLoader<any>[];
48
+ attributeLoaders: AttributeLoader<any>[];
49
+ constructor(options: ViewOptions, injector: Injector);
50
+ parseDoc(html: string, rootComponentLoader: ComponentLoader): Slot<any> | ComponentInstance<import("@textbus/core").ComponentExtends, any, unknown>;
51
+ parse(html: string, rootSlot: Slot): Slot<any>;
52
+ private readComponent;
53
+ private readFormats;
54
+ private readSlot;
55
+ private applyFormats;
56
+ }