@textbus/platform-browser 4.0.0-alpha.0 → 4.0.0-alpha.5

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.
@@ -1,29 +1,58 @@
1
- import { ComponentInstance, Slot, ViewAdapter, NodeLocation, VElement } from '@textbus/core';
1
+ import { ComponentInstance, Slot, ViewAdapter, NodeLocation, VElement, VTextNode } from '@textbus/core';
2
2
  /**
3
- * Textbus PC 端浏览器渲染能力实现
3
+ * Textbus PC 端浏览器渲染能力桥接器抽象类,提供了 DOM 元素查询能力,具体渲染能力由各前端框架实现相应桥接
4
4
  */
5
- export declare abstract class DomAdapter<ViewComponent, ViewElement> extends ViewAdapter<ViewElement> {
5
+ export declare abstract class DomAdapter<ViewComponent, ViewElement> extends ViewAdapter {
6
6
  private mount;
7
7
  host: HTMLElement;
8
- protected componentRootElementCaches: WeakMap<ComponentInstance<unknown, unknown, unknown>, HTMLElement>;
8
+ protected componentRootElementCaches: {
9
+ set: {
10
+ (key: ComponentInstance<unknown, unknown, unknown>, value: HTMLElement): void;
11
+ (key: HTMLElement, value: ComponentInstance<unknown, unknown, unknown>): void;
12
+ };
13
+ get: {
14
+ (key: ComponentInstance<unknown, unknown, unknown>): HTMLElement;
15
+ (key: HTMLElement): ComponentInstance<unknown, unknown, unknown>;
16
+ };
17
+ remove: (key: HTMLElement | ComponentInstance<unknown, unknown, unknown>) => void;
18
+ };
9
19
  protected slotRootNativeElementCaches: {
10
20
  set: {
11
- (key: Slot<any>, value: Node): void;
12
- (key: Node, value: Slot<any>): void;
21
+ (key: Slot<any>, value: HTMLElement): void;
22
+ (key: HTMLElement, value: Slot<any>): void;
13
23
  };
14
24
  get: {
15
- (key: Slot<any>): Node;
16
- (key: Node): Slot<any>;
25
+ (key: Slot<any>): HTMLElement;
26
+ (key: HTMLElement): Slot<any>;
17
27
  };
18
- remove: (key: Node | Slot<any>) => void;
28
+ remove: (key: HTMLElement | Slot<any>) => void;
19
29
  };
20
30
  protected slotRootVElementCaches: WeakMap<Slot<any>, VElement>;
21
31
  protected constructor(mount: (host: HTMLElement, viewComponent: ViewComponent) => (void | (() => void)));
22
32
  render(rootComponent: ComponentInstance): void | (() => void);
23
33
  abstract componentRender(component: ComponentInstance): ViewComponent;
34
+ abstract slotRender(slot: Slot, slotHostRender: (children: Array<VElement | VTextNode | ComponentInstance>) => VElement, renderEnv: any): ViewElement;
24
35
  copy(): void;
36
+ /**
37
+ * 根据组件获取组件的根 DOM 节点
38
+ * @param component
39
+ */
25
40
  getNativeNodeByComponent(component: ComponentInstance): HTMLElement | null;
41
+ /**
42
+ * 根据 DOM 节点,获对对应的组件根节点,如传入的 DOM 节点不为组件的根节点,则返回 null
43
+ * @param node
44
+ */
45
+ getComponentByNativeNode(node: HTMLElement): ComponentInstance | null;
46
+ /**
47
+ * 根据插槽获取插槽的根 DOM 节点
48
+ * @param slot
49
+ */
26
50
  getNativeNodeBySlot(slot: Slot): HTMLElement | null;
51
+ /**
52
+ * 根据 DOM 节点,获对对应的插槽根节点,如传入的 DOM 节点不为插槽的根节点,则返回 null
53
+ * @param node
54
+ */
55
+ getSlotByNativeNode(node: HTMLElement): Slot | null;
27
56
  /**
28
57
  * 获取插槽内容节点集合
29
58
  * @param slot
@@ -1,5 +1,5 @@
1
1
  import 'reflect-metadata';
2
- import { Slot, ViewAdapter, createBidirectionalMapping, VElement, VTextNode, ComponentInstance, Controller, Selection, RootComponentRef, ContentType, Event, invokeListener, Keyboard, Commander, Scheduler, NativeSelectionBridge } from '@textbus/core';
2
+ import { Slot, ViewAdapter, createBidirectionalMapping, ComponentInstance, VElement, VTextNode, Controller, Selection, RootComponentRef, ContentType, Event, invokeListener, Keyboard, Commander, Scheduler, NativeSelectionBridge } from '@textbus/core';
3
3
  import { InjectionToken, Injectable, Inject, Injector, Optional } from '@viewfly/core';
4
4
  import { Subject, filter, fromEvent, Subscription, distinctUntilChanged, merge, map, Observable } from '@tanbo/stream';
5
5
 
@@ -187,23 +187,23 @@ let Parser = Parser_1 = class Parser {
187
187
  this.attributeLoaders = attributeLoaders;
188
188
  }
189
189
  /**
190
- * 使用指定的组件加载器解析一段 HTML 字符串
190
+ * 使用指定的组件加载器解析一段 HTML 字符串或 DOM 元素
191
191
  * @param html
192
192
  * @param rootComponentLoader
193
193
  */
194
194
  parseDoc(html, rootComponentLoader) {
195
- const element = Parser_1.parseHTML(html);
195
+ const element = typeof html === 'string' ? Parser_1.parseHTML(html) : html;
196
196
  return rootComponentLoader.read(element, this.injector, (childSlot, slotRootElement, slotContentHostElement = slotRootElement) => {
197
197
  return this.readSlot(childSlot, slotRootElement, slotContentHostElement);
198
198
  });
199
199
  }
200
200
  /**
201
- * 将一段 HTML 解析到指定插槽
201
+ * 将一段 HTML 或 DOM 元素解析到指定插槽
202
202
  * @param html
203
203
  * @param rootSlot
204
204
  */
205
205
  parse(html, rootSlot) {
206
- const element = Parser_1.parseHTML(html);
206
+ const element = typeof html === 'string' ? Parser_1.parseHTML(html) : html;
207
207
  return this.readFormats(element, rootSlot);
208
208
  }
209
209
  readComponent(el, slot) {
@@ -301,7 +301,7 @@ class Input {
301
301
  }
302
302
 
303
303
  /**
304
- * Textbus PC 端浏览器渲染能力实现
304
+ * Textbus PC 端浏览器渲染能力桥接器抽象类,提供了 DOM 元素查询能力,具体渲染能力由各前端框架实现相应桥接
305
305
  */
306
306
  class DomAdapter extends ViewAdapter {
307
307
  constructor(mount) {
@@ -322,7 +322,9 @@ class DomAdapter extends ViewAdapter {
322
322
  id: 'textbus-' + Number((Math.random() + '').substring(2)).toString(16)
323
323
  }
324
324
  });
325
- this.componentRootElementCaches = new WeakMap();
325
+ this.componentRootElementCaches = createBidirectionalMapping(a => {
326
+ return a instanceof ComponentInstance;
327
+ });
326
328
  this.slotRootNativeElementCaches = createBidirectionalMapping(a => {
327
329
  return a instanceof Slot;
328
330
  });
@@ -335,12 +337,34 @@ class DomAdapter extends ViewAdapter {
335
337
  copy() {
336
338
  document.execCommand('copy');
337
339
  }
340
+ /**
341
+ * 根据组件获取组件的根 DOM 节点
342
+ * @param component
343
+ */
338
344
  getNativeNodeByComponent(component) {
339
345
  return this.componentRootElementCaches.get(component) || null;
340
346
  }
347
+ /**
348
+ * 根据 DOM 节点,获对对应的组件根节点,如传入的 DOM 节点不为组件的根节点,则返回 null
349
+ * @param node
350
+ */
351
+ getComponentByNativeNode(node) {
352
+ return this.componentRootElementCaches.get(node) || null;
353
+ }
354
+ /**
355
+ * 根据插槽获取插槽的根 DOM 节点
356
+ * @param slot
357
+ */
341
358
  getNativeNodeBySlot(slot) {
342
359
  return this.slotRootNativeElementCaches.get(slot) || null;
343
360
  }
361
+ /**
362
+ * 根据 DOM 节点,获对对应的插槽根节点,如传入的 DOM 节点不为插槽的根节点,则返回 null
363
+ * @param node
364
+ */
365
+ getSlotByNativeNode(node) {
366
+ return this.slotRootNativeElementCaches.get(node) || null;
367
+ }
344
368
  /**
345
369
  * 获取插槽内容节点集合
346
370
  * @param slot
@@ -1364,17 +1388,14 @@ let MagicInput = class MagicInput extends Input {
1364
1388
  this.doc.body.appendChild(div);
1365
1389
  div.focus();
1366
1390
  setTimeout(() => {
1367
- let html = div.innerHTML;
1368
- if (!html && text && this.isFirefox) {
1369
- html = text;
1370
- }
1371
- this.handlePaste(html, text);
1372
1391
  this.doc.body.removeChild(div);
1392
+ div.style.cssText = '';
1393
+ this.handlePaste(div, text);
1373
1394
  });
1374
1395
  }));
1375
1396
  }
1376
- handlePaste(html, text) {
1377
- const slot = this.parser.parse(html, new Slot([
1397
+ handlePaste(dom, text) {
1398
+ const slot = this.parser.parse(dom, new Slot([
1378
1399
  ContentType.BlockComponent,
1379
1400
  ContentType.InlineComponent,
1380
1401
  ContentType.Text
@@ -2014,9 +2035,9 @@ let NativeInput = class NativeInput extends Input {
2014
2035
  document.body.appendChild(div);
2015
2036
  div.focus();
2016
2037
  setTimeout(() => {
2017
- const html = div.innerHTML;
2018
- this.handlePaste(html, text);
2019
2038
  document.body.removeChild(div);
2039
+ div.style.cssText = '';
2040
+ this.handlePaste(div, text);
2020
2041
  });
2021
2042
  }));
2022
2043
  }
package/bundles/index.js CHANGED
@@ -189,23 +189,23 @@ exports.Parser = Parser_1 = class Parser {
189
189
  this.attributeLoaders = attributeLoaders;
190
190
  }
191
191
  /**
192
- * 使用指定的组件加载器解析一段 HTML 字符串
192
+ * 使用指定的组件加载器解析一段 HTML 字符串或 DOM 元素
193
193
  * @param html
194
194
  * @param rootComponentLoader
195
195
  */
196
196
  parseDoc(html, rootComponentLoader) {
197
- const element = Parser_1.parseHTML(html);
197
+ const element = typeof html === 'string' ? Parser_1.parseHTML(html) : html;
198
198
  return rootComponentLoader.read(element, this.injector, (childSlot, slotRootElement, slotContentHostElement = slotRootElement) => {
199
199
  return this.readSlot(childSlot, slotRootElement, slotContentHostElement);
200
200
  });
201
201
  }
202
202
  /**
203
- * 将一段 HTML 解析到指定插槽
203
+ * 将一段 HTML 或 DOM 元素解析到指定插槽
204
204
  * @param html
205
205
  * @param rootSlot
206
206
  */
207
207
  parse(html, rootSlot) {
208
- const element = Parser_1.parseHTML(html);
208
+ const element = typeof html === 'string' ? Parser_1.parseHTML(html) : html;
209
209
  return this.readFormats(element, rootSlot);
210
210
  }
211
211
  readComponent(el, slot) {
@@ -303,7 +303,7 @@ class Input {
303
303
  }
304
304
 
305
305
  /**
306
- * Textbus PC 端浏览器渲染能力实现
306
+ * Textbus PC 端浏览器渲染能力桥接器抽象类,提供了 DOM 元素查询能力,具体渲染能力由各前端框架实现相应桥接
307
307
  */
308
308
  class DomAdapter extends core$1.ViewAdapter {
309
309
  constructor(mount) {
@@ -324,7 +324,9 @@ class DomAdapter extends core$1.ViewAdapter {
324
324
  id: 'textbus-' + Number((Math.random() + '').substring(2)).toString(16)
325
325
  }
326
326
  });
327
- this.componentRootElementCaches = new WeakMap();
327
+ this.componentRootElementCaches = core$1.createBidirectionalMapping(a => {
328
+ return a instanceof core$1.ComponentInstance;
329
+ });
328
330
  this.slotRootNativeElementCaches = core$1.createBidirectionalMapping(a => {
329
331
  return a instanceof core$1.Slot;
330
332
  });
@@ -337,12 +339,34 @@ class DomAdapter extends core$1.ViewAdapter {
337
339
  copy() {
338
340
  document.execCommand('copy');
339
341
  }
342
+ /**
343
+ * 根据组件获取组件的根 DOM 节点
344
+ * @param component
345
+ */
340
346
  getNativeNodeByComponent(component) {
341
347
  return this.componentRootElementCaches.get(component) || null;
342
348
  }
349
+ /**
350
+ * 根据 DOM 节点,获对对应的组件根节点,如传入的 DOM 节点不为组件的根节点,则返回 null
351
+ * @param node
352
+ */
353
+ getComponentByNativeNode(node) {
354
+ return this.componentRootElementCaches.get(node) || null;
355
+ }
356
+ /**
357
+ * 根据插槽获取插槽的根 DOM 节点
358
+ * @param slot
359
+ */
343
360
  getNativeNodeBySlot(slot) {
344
361
  return this.slotRootNativeElementCaches.get(slot) || null;
345
362
  }
363
+ /**
364
+ * 根据 DOM 节点,获对对应的插槽根节点,如传入的 DOM 节点不为插槽的根节点,则返回 null
365
+ * @param node
366
+ */
367
+ getSlotByNativeNode(node) {
368
+ return this.slotRootNativeElementCaches.get(node) || null;
369
+ }
346
370
  /**
347
371
  * 获取插槽内容节点集合
348
372
  * @param slot
@@ -1366,17 +1390,14 @@ exports.MagicInput = class MagicInput extends Input {
1366
1390
  this.doc.body.appendChild(div);
1367
1391
  div.focus();
1368
1392
  setTimeout(() => {
1369
- let html = div.innerHTML;
1370
- if (!html && text && this.isFirefox) {
1371
- html = text;
1372
- }
1373
- this.handlePaste(html, text);
1374
1393
  this.doc.body.removeChild(div);
1394
+ div.style.cssText = '';
1395
+ this.handlePaste(div, text);
1375
1396
  });
1376
1397
  }));
1377
1398
  }
1378
- handlePaste(html, text) {
1379
- const slot = this.parser.parse(html, new core$1.Slot([
1399
+ handlePaste(dom, text) {
1400
+ const slot = this.parser.parse(dom, new core$1.Slot([
1380
1401
  core$1.ContentType.BlockComponent,
1381
1402
  core$1.ContentType.InlineComponent,
1382
1403
  core$1.ContentType.Text
@@ -2016,9 +2037,9 @@ let NativeInput = class NativeInput extends Input {
2016
2037
  document.body.appendChild(div);
2017
2038
  div.focus();
2018
2039
  setTimeout(() => {
2019
- const html = div.innerHTML;
2020
- this.handlePaste(html, text);
2021
2040
  document.body.removeChild(div);
2041
+ div.style.cssText = '';
2042
+ this.handlePaste(div, text);
2022
2043
  });
2023
2044
  }));
2024
2045
  }
@@ -73,17 +73,17 @@ export declare class Parser {
73
73
  attributeLoaders: AttributeLoader<any>[];
74
74
  constructor(options: ViewOptions, injector: Injector);
75
75
  /**
76
- * 使用指定的组件加载器解析一段 HTML 字符串
76
+ * 使用指定的组件加载器解析一段 HTML 字符串或 DOM 元素
77
77
  * @param html
78
78
  * @param rootComponentLoader
79
79
  */
80
- parseDoc(html: string, rootComponentLoader: ComponentLoader): void | Slot<any> | ComponentInstance<unknown, unknown, unknown>;
80
+ parseDoc(html: string | HTMLElement, rootComponentLoader: ComponentLoader): void | Slot<any> | ComponentInstance<unknown, unknown, unknown>;
81
81
  /**
82
- * 将一段 HTML 解析到指定插槽
82
+ * 将一段 HTML 或 DOM 元素解析到指定插槽
83
83
  * @param html
84
84
  * @param rootSlot
85
85
  */
86
- parse(html: string, rootSlot: Slot): Slot<any>;
86
+ parse(html: string | HTMLElement, rootSlot: Slot): Slot<any>;
87
87
  private readComponent;
88
88
  private readText;
89
89
  private readFormats;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textbus/platform-browser",
3
- "version": "4.0.0-alpha.0",
3
+ "version": "4.0.0-alpha.5",
4
4
  "description": "Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -26,8 +26,8 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "@tanbo/stream": "^1.2.0",
29
- "@textbus/core": "^4.0.0-alpha.0",
30
- "@viewfly/core": "^0.1.0",
29
+ "@textbus/core": "^4.0.0-alpha.5",
30
+ "@viewfly/core": "^0.2.4",
31
31
  "reflect-metadata": "^0.1.13"
32
32
  },
33
33
  "devDependencies": {
@@ -48,5 +48,5 @@
48
48
  "bugs": {
49
49
  "url": "https://github.com/textbus/textbus.git/issues"
50
50
  },
51
- "gitHead": "cf4fd289b73bc777124a32fe42bb58eba05a34f1"
51
+ "gitHead": "072831527fe07e7c21a14f3141d55b24d321a690"
52
52
  }