@difizen/libro-toc 0.0.2-alpha.0

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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/es/cell-toc-provider.d.ts +9 -0
  4. package/es/cell-toc-provider.d.ts.map +1 -0
  5. package/es/cell-toc-provider.js +46 -0
  6. package/es/index.d.ts +6 -0
  7. package/es/index.d.ts.map +1 -0
  8. package/es/index.js +5 -0
  9. package/es/index.less +66 -0
  10. package/es/libro-toc-color-registry.d.ts +6 -0
  11. package/es/libro-toc-color-registry.d.ts.map +1 -0
  12. package/es/libro-toc-color-registry.js +46 -0
  13. package/es/module.d.ts +14 -0
  14. package/es/module.d.ts.map +1 -0
  15. package/es/module.js +40 -0
  16. package/es/provider/html.d.ts +39 -0
  17. package/es/provider/html.d.ts.map +1 -0
  18. package/es/provider/html.js +54 -0
  19. package/es/provider/markdown-toc-provider.d.ts +11 -0
  20. package/es/provider/markdown-toc-provider.d.ts.map +1 -0
  21. package/es/provider/markdown-toc-provider.js +43 -0
  22. package/es/provider/markdown.d.ts +23 -0
  23. package/es/provider/markdown.d.ts.map +1 -0
  24. package/es/provider/markdown.js +142 -0
  25. package/es/provider/output-toc-provider.d.ts +10 -0
  26. package/es/provider/output-toc-provider.d.ts.map +1 -0
  27. package/es/provider/output-toc-provider.js +67 -0
  28. package/es/toc-collapse-service.d.ts +10 -0
  29. package/es/toc-collapse-service.d.ts.map +1 -0
  30. package/es/toc-collapse-service.js +141 -0
  31. package/es/toc-configuration.d.ts +7 -0
  32. package/es/toc-configuration.d.ts.map +1 -0
  33. package/es/toc-configuration.js +34 -0
  34. package/es/toc-contribution.d.ts +11 -0
  35. package/es/toc-contribution.d.ts.map +1 -0
  36. package/es/toc-contribution.js +63 -0
  37. package/es/toc-manager.d.ts +10 -0
  38. package/es/toc-manager.d.ts.map +1 -0
  39. package/es/toc-manager.js +32 -0
  40. package/es/toc-protocol.d.ts +107 -0
  41. package/es/toc-protocol.d.ts.map +1 -0
  42. package/es/toc-protocol.js +35 -0
  43. package/es/toc-provider.d.ts +37 -0
  44. package/es/toc-provider.d.ts.map +1 -0
  45. package/es/toc-provider.js +181 -0
  46. package/es/toc-view.d.ts +33 -0
  47. package/es/toc-view.d.ts.map +1 -0
  48. package/es/toc-view.js +245 -0
  49. package/package.json +62 -0
  50. package/src/cell-toc-provider.ts +31 -0
  51. package/src/index.less +66 -0
  52. package/src/index.ts +5 -0
  53. package/src/libro-toc-color-registry.ts +27 -0
  54. package/src/module.ts +58 -0
  55. package/src/provider/html.ts +61 -0
  56. package/src/provider/markdown-toc-provider.ts +34 -0
  57. package/src/provider/markdown.ts +182 -0
  58. package/src/provider/output-toc-provider.ts +53 -0
  59. package/src/toc-collapse-service.ts +96 -0
  60. package/src/toc-configuration.ts +22 -0
  61. package/src/toc-contribution.ts +34 -0
  62. package/src/toc-manager.ts +27 -0
  63. package/src/toc-protocol.ts +130 -0
  64. package/src/toc-provider.ts +154 -0
  65. package/src/toc-view.tsx +225 -0
@@ -0,0 +1,181 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ var _dec, _dec2, _dec3, _dec4, _class, _class2, _descriptor, _descriptor2, _descriptor3;
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
12
+ function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
13
+ function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
14
+ import { inject, notEmpty, prop, transient, watch } from '@difizen/mana-app';
15
+ import { Emitter, DisposableCollection } from '@difizen/mana-app';
16
+ import { LibroCellTOCProvider } from "./cell-toc-provider.js";
17
+ import { TOCProviderOption } from "./toc-protocol.js";
18
+ export var LibroTOCProviderFactory = Symbol('LibroTOCProviderFactory');
19
+ export var LibroTOCProvider = (_dec = transient(), _dec2 = prop(), _dec3 = prop(), _dec4 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () {
20
+ function LibroTOCProvider(option, libroCellTOCProvider) {
21
+ var _this = this;
22
+ _classCallCheck(this, LibroTOCProvider);
23
+ _initializerDefineProperty(this, "providerMap", _descriptor, this);
24
+ _initializerDefineProperty(this, "headings", _descriptor2, this);
25
+ _initializerDefineProperty(this, "view", _descriptor3, this);
26
+ this.toDispose = new DisposableCollection();
27
+ this.toDisposeWatcher = new DisposableCollection();
28
+ this.disposed = false;
29
+ this.activeCellChangeEmitter = new Emitter();
30
+ this.handleActiveCellChange = function () {
31
+ var header = _this.getHeadingByCellIndex(_this.view.model.activeIndex);
32
+ if (header) {
33
+ _this.activeCellChangeEmitter.fire(header);
34
+ }
35
+ };
36
+ this.updateTOC = function () {
37
+ _this.headings = _this.getHeadings();
38
+ };
39
+ this.onActiveCellChanged = function () {
40
+ _this.updateTOC();
41
+ };
42
+ this.onCellsChanged = function () {
43
+ _this.setupUpdaterWatcher();
44
+ _this.updateTOC();
45
+ };
46
+ this.onContentChanged = function () {
47
+ _this.updateTOC();
48
+ };
49
+ this.view = option.view;
50
+ this.libroCellTOCProvider = libroCellTOCProvider;
51
+ this.initUpdateWatch();
52
+ this.updateTOC();
53
+ }
54
+ LibroTOCProvider = inject(LibroCellTOCProvider)(LibroTOCProvider, undefined, 1) || LibroTOCProvider;
55
+ LibroTOCProvider = inject(TOCProviderOption)(LibroTOCProvider, undefined, 0) || LibroTOCProvider;
56
+ _createClass(LibroTOCProvider, [{
57
+ key: "activeCellChange",
58
+ get: function get() {
59
+ return this.activeCellChangeEmitter.event;
60
+ }
61
+ }, {
62
+ key: "initUpdateWatch",
63
+ value: function initUpdateWatch() {
64
+ this.toDispose.push(watch(this.view.model, 'activeIndex', this.handleActiveCellChange));
65
+ this.toDispose.push(watch(this.view.model, 'cells', this.onCellsChanged));
66
+ this.setupUpdaterWatcher();
67
+ }
68
+ }, {
69
+ key: "setupUpdaterWatcher",
70
+ value: function setupUpdaterWatcher() {
71
+ var _this2 = this;
72
+ this.toDisposeWatcher.dispose();
73
+ this.toDisposeWatcher = new DisposableCollection();
74
+ this.getCellTocProviders().map(function (item) {
75
+ _this2.toDisposeWatcher.push(item.updateWatcher(_this2.updateTOC));
76
+ });
77
+ }
78
+ }, {
79
+ key: "getCellTocProviderList",
80
+ value: function getCellTocProviderList() {
81
+ var _this3 = this;
82
+ if (!this.view) {
83
+ return [];
84
+ }
85
+ var cells = this.view.model.cells;
86
+ return cells.map(function (cell) {
87
+ var tocProvider;
88
+ if (_this3.providerMap.has(cell.id)) {
89
+ tocProvider = _this3.providerMap.get(cell.id);
90
+ } else {
91
+ tocProvider = _this3.libroCellTOCProvider.createCellTOCProvider(cell);
92
+ }
93
+ if (tocProvider) {
94
+ _this3.providerMap.set(cell.id, tocProvider);
95
+ }
96
+ return {
97
+ cellId: cell.model.id,
98
+ tocProvider: tocProvider
99
+ };
100
+ });
101
+ }
102
+ }, {
103
+ key: "getCellTocProviders",
104
+ value: function getCellTocProviders() {
105
+ return this.getCellTocProviderList().map(function (item) {
106
+ return item.tocProvider;
107
+ }).filter(notEmpty);
108
+ }
109
+ }, {
110
+ key: "getHeadings",
111
+ value: function getHeadings() {
112
+ return this.getCellTocProviderList().map(function (item) {
113
+ if (item.tocProvider !== undefined) {
114
+ var headings = item.tocProvider.getHeadings();
115
+ headings.forEach(function (heading) {
116
+ return heading.dataset = _objectSpread(_objectSpread({}, heading.dataset), {}, {
117
+ cellId: item.cellId
118
+ });
119
+ });
120
+ return headings;
121
+ }
122
+ return;
123
+ }).filter(notEmpty).flat();
124
+ }
125
+ }, {
126
+ key: "selectCellByHeading",
127
+ value: function selectCellByHeading(heading) {
128
+ var _heading$dataset;
129
+ var cellId = heading === null || heading === void 0 ? void 0 : (_heading$dataset = heading.dataset) === null || _heading$dataset === void 0 ? void 0 : _heading$dataset['cellId'];
130
+ if (!cellId) {
131
+ return;
132
+ }
133
+ var cell = this.view.model.cells.find(function (item) {
134
+ return item.model.id === cellId;
135
+ });
136
+ if (cell) {
137
+ this.view.selectCell(cell);
138
+ }
139
+ }
140
+ }, {
141
+ key: "getHeadingByCellIndex",
142
+ value: function getHeadingByCellIndex(index) {
143
+ var cell = this.view.model.cells[index];
144
+ return this.headings.find(function (item) {
145
+ var _item$dataset;
146
+ return (item === null || item === void 0 ? void 0 : (_item$dataset = item.dataset) === null || _item$dataset === void 0 ? void 0 : _item$dataset['cellId']) === cell.model.id;
147
+ });
148
+ }
149
+ }, {
150
+ key: "dispose",
151
+ value: function dispose() {
152
+ if (this.disposed) {
153
+ return;
154
+ }
155
+ this.toDispose.dispose();
156
+ this.toDisposeWatcher.dispose();
157
+ this.providerMap.clear();
158
+ this.disposed = true;
159
+ }
160
+ }]);
161
+ return LibroTOCProvider;
162
+ }(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "providerMap", [_dec2], {
163
+ configurable: true,
164
+ enumerable: true,
165
+ writable: true,
166
+ initializer: function initializer() {
167
+ return new Map();
168
+ }
169
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "headings", [_dec3], {
170
+ configurable: true,
171
+ enumerable: true,
172
+ writable: true,
173
+ initializer: function initializer() {
174
+ return [];
175
+ }
176
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "view", [_dec4], {
177
+ configurable: true,
178
+ enumerable: true,
179
+ writable: true,
180
+ initializer: null
181
+ })), _class2)) || _class);
@@ -0,0 +1,33 @@
1
+ import type { DisplayView, LibroView } from '@difizen/libro-core';
2
+ import { ConfigurationService } from '@difizen/mana-app';
3
+ import { BaseView } from '@difizen/mana-app';
4
+ import { LibroTOCManager } from './toc-manager.js';
5
+ import type { IHeading } from './toc-protocol.js';
6
+ import type { LibroTOCProvider } from './toc-provider.js';
7
+ import './index.less';
8
+ interface DisplayHeading extends IHeading {
9
+ hasChild: boolean;
10
+ visible: boolean;
11
+ }
12
+ export declare const TocRender: () => import("react/jsx-runtime").JSX.Element;
13
+ export declare class TOCView extends BaseView implements DisplayView {
14
+ parent: LibroView | undefined;
15
+ protected configurationService: ConfigurationService;
16
+ view: () => import("react/jsx-runtime").JSX.Element;
17
+ isDisplay: boolean;
18
+ tocProvider?: LibroTOCProvider;
19
+ activeHeading: IHeading | undefined;
20
+ protected libroTOCManager: LibroTOCManager;
21
+ tocTitle: string;
22
+ headingCollapseState: Map<string, boolean>;
23
+ constructor(libroTOCManager: LibroTOCManager, configurationService: ConfigurationService);
24
+ getHeadingIndentSize(level: number): number;
25
+ onViewMount(): void;
26
+ getDisplayHeadings(): DisplayHeading[];
27
+ protected isHeadingVisible(heading: IHeading, index: number, list: IHeading[]): boolean;
28
+ isHeadingCollapsed(heading: IHeading): boolean;
29
+ protected hasChildren(current: IHeading, index: number, list: IHeading[]): boolean;
30
+ dispose(): void;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=toc-view.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toc-view.d.ts","sourceRoot":"","sources":["../src/toc-view.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,QAAQ,EAAsB,MAAM,mBAAmB,CAAC;AAMjE,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,cAAc,CAAC;AAEtB,UAAU,cAAe,SAAQ,QAAQ;IACvC,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;CAClB;AA6DD,eAAO,MAAM,SAAS,+CA+BrB,CAAC;AAEF,qBAEa,OAAQ,SAAQ,QAAS,YAAW,WAAW;IAC1D,MAAM,EAAE,SAAS,GAAG,SAAS,CAAa;IAC1C,SAAS,CAAC,oBAAoB,EAAE,oBAAoB,CAAC;IAE5C,IAAI,gDAAa;IAG1B,SAAS,EAAE,OAAO,CAAC;IAGnB,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAG/B,aAAa,EAAE,QAAQ,GAAG,SAAS,CAAC;IAEpC,SAAS,CAAC,eAAe,EAAE,eAAe,CAAC;IAG3C,QAAQ,EAAE,MAAM,CAAgB;IAGhC,oBAAoB,uBAA8B;gBAGvB,eAAe,EAAE,eAAe,EAC3B,oBAAoB,EAAE,oBAAoB;IAgB1E,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIlC,WAAW;IAgBpB,kBAAkB,IAAI,cAAc,EAAE;IAYtC,SAAS,CAAC,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;IAoB7E,kBAAkB,CAAC,OAAO,EAAE,QAAQ;IAIpC,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;IAU/D,OAAO;CAGjB"}
package/es/toc-view.js ADDED
@@ -0,0 +1,245 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5;
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
12
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
14
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
15
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
16
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
17
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
18
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
19
+ function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
20
+ function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
21
+ import { ArrowDown, ArrowRight } from '@difizen/libro-core';
22
+ import { ConfigurationService } from '@difizen/mana-app';
23
+ import { getOrigin, prop, useInject } from '@difizen/mana-app';
24
+ import { BaseView, view, ViewInstance } from '@difizen/mana-app';
25
+ import { inject, transient } from '@difizen/mana-app';
26
+ import { l10n } from '@difizen/mana-l10n';
27
+ import React, { useRef } from 'react';
28
+ import { TOCVisible } from "./toc-configuration.js";
29
+ import { LibroTOCManager } from "./toc-manager.js";
30
+ import "./index.less";
31
+ import { jsx as _jsx } from "react/jsx-runtime";
32
+ import { jsxs as _jsxs } from "react/jsx-runtime";
33
+ import { Fragment as _Fragment } from "react/jsx-runtime";
34
+ var TocItem = function TocItem(_ref) {
35
+ var heading = _ref.heading,
36
+ onClick = _ref.onClick,
37
+ active = _ref.active,
38
+ headingCollapsed = _ref.headingCollapsed,
39
+ onToggle = _ref.onToggle;
40
+ var instance = useInject(ViewInstance);
41
+ var id = heading.id,
42
+ text = heading.text,
43
+ level = heading.level,
44
+ hasChild = heading.hasChild,
45
+ visible = heading.visible;
46
+ if (!visible) {
47
+ return null;
48
+ }
49
+ var handleClick = function handleClick() {
50
+ if (!id) {
51
+ return;
52
+ }
53
+ var element = document.getElementById(id);
54
+ if (element) {
55
+ element.scrollIntoView();
56
+ }
57
+ onClick(heading);
58
+ };
59
+ return /*#__PURE__*/_jsxs(_Fragment, {
60
+ children: [/*#__PURE__*/_jsxs("div", {
61
+ className: "markdown-toc-container-anchor ".concat(active ? 'active' : ''),
62
+ style: {
63
+ paddingLeft: instance.getHeadingIndentSize(level) + (hasChild ? 0 : 20)
64
+ },
65
+ onClick: handleClick,
66
+ children: [hasChild && /*#__PURE__*/_jsx("span", {
67
+ onClick: function onClick(e) {
68
+ e.stopPropagation();
69
+ onToggle();
70
+ },
71
+ className: "libro-toc-collapsed",
72
+ children: headingCollapsed ? /*#__PURE__*/_jsx(ArrowRight, {}) : /*#__PURE__*/_jsx(ArrowDown, {})
73
+ }), text]
74
+ }), /*#__PURE__*/_jsx("div", {
75
+ className: "markdown-toc-container-anchor-shot ".concat(active ? 'active' : '')
76
+ })]
77
+ });
78
+ };
79
+ export var TocRender = function TocRender() {
80
+ var instance = useInject(ViewInstance);
81
+ var containRef = useRef(null);
82
+ var handleClick = function handleClick(heading) {
83
+ var _instance$tocProvider;
84
+ instance.activeHeading = heading;
85
+ (_instance$tocProvider = instance.tocProvider) === null || _instance$tocProvider === void 0 ? void 0 : _instance$tocProvider.selectCellByHeading(heading);
86
+ };
87
+ return /*#__PURE__*/_jsxs("div", {
88
+ className: "markdown-toc-container",
89
+ ref: containRef,
90
+ children: [/*#__PURE__*/_jsx("div", {
91
+ className: "markdown-toc-container-title",
92
+ children: instance.tocTitle
93
+ }), instance.getDisplayHeadings().map(function (heading) {
94
+ var _instance$activeHeadi;
95
+ var collapsed = instance.isHeadingCollapsed(heading);
96
+ return /*#__PURE__*/_jsx(TocItem, {
97
+ active: ((_instance$activeHeadi = instance.activeHeading) === null || _instance$activeHeadi === void 0 ? void 0 : _instance$activeHeadi.id) === heading.id,
98
+ heading: heading,
99
+ onClick: handleClick,
100
+ headingCollapsed: collapsed,
101
+ onToggle: function onToggle() {
102
+ if (heading.id) {
103
+ instance.headingCollapseState.set(heading.id, !collapsed);
104
+ }
105
+ }
106
+ }, heading.id);
107
+ })]
108
+ });
109
+ };
110
+ export var TOCView = (_dec = transient(), _dec2 = view('libro-toc-view'), _dec3 = prop(), _dec4 = prop(), _dec5 = prop(), _dec6 = prop(), _dec7 = prop(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_BaseView) {
111
+ _inherits(TOCView, _BaseView);
112
+ var _super = _createSuper(TOCView);
113
+ function TOCView(libroTOCManager, configurationService) {
114
+ var _this;
115
+ _classCallCheck(this, TOCView);
116
+ _this = _super.call(this);
117
+ _this.parent = undefined;
118
+ _this.view = TocRender;
119
+ _initializerDefineProperty(_this, "isDisplay", _descriptor, _assertThisInitialized(_this));
120
+ _initializerDefineProperty(_this, "tocProvider", _descriptor2, _assertThisInitialized(_this));
121
+ _initializerDefineProperty(_this, "activeHeading", _descriptor3, _assertThisInitialized(_this));
122
+ _initializerDefineProperty(_this, "tocTitle", _descriptor4, _assertThisInitialized(_this));
123
+ _initializerDefineProperty(_this, "headingCollapseState", _descriptor5, _assertThisInitialized(_this));
124
+ _this.libroTOCManager = libroTOCManager;
125
+ _this.configurationService = configurationService;
126
+ _this.configurationService.get(TOCVisible).then(function (value) {
127
+ _this.isDisplay = value;
128
+ return;
129
+ }).catch(function () {
130
+ //
131
+ });
132
+ return _this;
133
+ }
134
+ TOCView = inject(ConfigurationService)(TOCView, undefined, 1) || TOCView;
135
+ TOCView = inject(LibroTOCManager)(TOCView, undefined, 0) || TOCView;
136
+ _createClass(TOCView, [{
137
+ key: "getHeadingIndentSize",
138
+ value: function getHeadingIndentSize(level) {
139
+ return level * 12;
140
+ }
141
+ }, {
142
+ key: "onViewMount",
143
+ value: function onViewMount() {
144
+ var _this2 = this;
145
+ if (!this.tocProvider && this.parent) {
146
+ getOrigin(this.parent.initialized).then(function () {
147
+ _this2.tocProvider = _this2.libroTOCManager.getTOCProvider(_this2.parent);
148
+ _this2.tocProvider.activeCellChange(function (header) {
149
+ _this2.activeHeading = header;
150
+ });
151
+ return;
152
+ }).catch(function () {
153
+ //
154
+ });
155
+ }
156
+ }
157
+ }, {
158
+ key: "getDisplayHeadings",
159
+ value: function getDisplayHeadings() {
160
+ var _this$tocProvider$hea,
161
+ _this$tocProvider,
162
+ _this3 = this;
163
+ var headings = (_this$tocProvider$hea = (_this$tocProvider = this.tocProvider) === null || _this$tocProvider === void 0 ? void 0 : _this$tocProvider.headings) !== null && _this$tocProvider$hea !== void 0 ? _this$tocProvider$hea : [];
164
+ return headings.map(function (item, index) {
165
+ return _objectSpread(_objectSpread({}, item), {}, {
166
+ visible: _this3.isHeadingVisible(item, index, headings),
167
+ hasChild: _this3.hasChildren(item, index, headings)
168
+ });
169
+ });
170
+ }
171
+ }, {
172
+ key: "isHeadingVisible",
173
+ value: function isHeadingVisible(heading, index, list) {
174
+ if (index === 0) {
175
+ return true;
176
+ }
177
+ var headingCollapsed = false;
178
+ var parent = heading;
179
+ for (var i = index - 1; i >= 0; i--) {
180
+ var current = list[i];
181
+ if (current.level < parent.level) {
182
+ parent = current;
183
+ if (this.isHeadingCollapsed(parent)) {
184
+ headingCollapsed = true;
185
+ break;
186
+ }
187
+ }
188
+ }
189
+ return !headingCollapsed;
190
+ }
191
+ }, {
192
+ key: "isHeadingCollapsed",
193
+ value: function isHeadingCollapsed(heading) {
194
+ var _this$headingCollapse;
195
+ return heading.id ? (_this$headingCollapse = this.headingCollapseState.get(heading.id)) !== null && _this$headingCollapse !== void 0 ? _this$headingCollapse : false : false;
196
+ }
197
+ }, {
198
+ key: "hasChildren",
199
+ value: function hasChildren(current, index, list) {
200
+ if (index === list.length - 1) {
201
+ return false;
202
+ }
203
+ if (current.level < list[index + 1].level) {
204
+ return true;
205
+ }
206
+ return false;
207
+ }
208
+ }, {
209
+ key: "dispose",
210
+ value: function dispose() {
211
+ var _this$tocProvider2;
212
+ (_this$tocProvider2 = this.tocProvider) === null || _this$tocProvider2 === void 0 ? void 0 : _this$tocProvider2.dispose();
213
+ }
214
+ }]);
215
+ return TOCView;
216
+ }(BaseView), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "isDisplay", [_dec3], {
217
+ configurable: true,
218
+ enumerable: true,
219
+ writable: true,
220
+ initializer: null
221
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "tocProvider", [_dec4], {
222
+ configurable: true,
223
+ enumerable: true,
224
+ writable: true,
225
+ initializer: null
226
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "activeHeading", [_dec5], {
227
+ configurable: true,
228
+ enumerable: true,
229
+ writable: true,
230
+ initializer: null
231
+ }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "tocTitle", [_dec6], {
232
+ configurable: true,
233
+ enumerable: true,
234
+ writable: true,
235
+ initializer: function initializer() {
236
+ return l10n.t('大纲');
237
+ }
238
+ }), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "headingCollapseState", [_dec7], {
239
+ configurable: true,
240
+ enumerable: true,
241
+ writable: true,
242
+ initializer: function initializer() {
243
+ return new Map();
244
+ }
245
+ })), _class2)) || _class) || _class);
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@difizen/libro-toc",
3
+ "version": "0.0.2-alpha.0",
4
+ "description": "",
5
+ "keywords": [
6
+ "libro",
7
+ "notebook",
8
+ "toc"
9
+ ],
10
+ "repository": "git@github.com:difizen/libro.git",
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "typings": "./es/index.d.ts",
16
+ "default": "./es/index.js"
17
+ },
18
+ "./mock": {
19
+ "typings": "./es/mock/index.d.ts",
20
+ "default": "./es/mock/index.js"
21
+ },
22
+ "./es/mock": {
23
+ "typings": "./es/mock/index.d.ts",
24
+ "default": "./es/mock/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "main": "es/index.js",
29
+ "module": "es/index.js",
30
+ "typings": "es/index.d.ts",
31
+ "files": [
32
+ "es",
33
+ "src"
34
+ ],
35
+ "dependencies": {
36
+ "@difizen/libro-common": "^0.0.2-alpha.0",
37
+ "@difizen/libro-core": "^0.0.2-alpha.0",
38
+ "@difizen/libro-markdown": "^0.0.2-alpha.0",
39
+ "@difizen/libro-rendermime": "^0.0.2-alpha.0",
40
+ "@difizen/mana-app": "alpha",
41
+ "@difizen/mana-l10n": "alpha"
42
+ },
43
+ "peerDependencies": {
44
+ "react": "^18.2.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/react": "^18.2.25"
48
+ },
49
+ "scripts": {
50
+ "setup": "father build",
51
+ "build": "father build",
52
+ "test": ": Note: lint task is delegated to test:* scripts",
53
+ "test:vitest": "vitest run",
54
+ "test:jest": "jest",
55
+ "coverage": ": Note: lint task is delegated to coverage:* scripts",
56
+ "coverage:vitest": "vitest run --coverage",
57
+ "coverage:jest": "jest --coverage",
58
+ "lint": ": Note: lint task is delegated to lint:* scripts",
59
+ "lint:eslint": "eslint src",
60
+ "lint:tsc": "tsc --noEmit"
61
+ }
62
+ }
@@ -0,0 +1,31 @@
1
+ import type { CellView } from '@difizen/libro-core';
2
+ import type { Contribution } from '@difizen/mana-app';
3
+ import { Priority } from '@difizen/mana-app';
4
+ import { contrib, singleton } from '@difizen/mana-app';
5
+
6
+ import { CellTOCProviderContribution } from './toc-protocol.js';
7
+
8
+ @singleton()
9
+ export class LibroCellTOCProvider {
10
+ @contrib(CellTOCProviderContribution)
11
+ protected providerContribution: Contribution.Provider<CellTOCProviderContribution>;
12
+
13
+ createCellTOCProvider(cell: CellView) {
14
+ const ctrb = this.findCellTOCProviderContribution(cell);
15
+ if (ctrb) {
16
+ return ctrb.factory(cell);
17
+ }
18
+ return;
19
+ }
20
+
21
+ protected findCellTOCProviderContribution(
22
+ cell: CellView,
23
+ ): CellTOCProviderContribution | undefined {
24
+ const prioritized = Priority.sortSync(
25
+ this.providerContribution.getContributions(),
26
+ (contribution) => contribution.canHandle(cell),
27
+ );
28
+ const sorted = prioritized.map((c) => c.value);
29
+ return sorted[0];
30
+ }
31
+ }
package/src/index.less ADDED
@@ -0,0 +1,66 @@
1
+ .libro-markdown-container {
2
+ width: 100%;
3
+ height: 100%;
4
+ height: 16px;
5
+ min-height: 16px;
6
+ }
7
+
8
+ .markdown-toc-container {
9
+ width: 120px;
10
+ min-height: 240px;
11
+ // position: absolute;
12
+ // top: 12px;
13
+ // right: 0;
14
+ // bottom: 0;
15
+ padding-top: 12px;
16
+ overflow-y: auto;
17
+
18
+ .markdown-toc-container-title {
19
+ box-sizing: border-box;
20
+ height: 24px;
21
+ margin-bottom: 16px;
22
+ color: var(--mana-libro-toc-title-color);
23
+ font-weight: 600;
24
+ font-size: 16px;
25
+ line-height: 24px;
26
+ letter-spacing: 0;
27
+ text-align: left;
28
+
29
+ .anticon-eye-invisible,
30
+ .anticon-eye {
31
+ padding-left: 4px;
32
+ cursor: pointer;
33
+ }
34
+ }
35
+
36
+ .markdown-toc-container-anchor {
37
+ position: relative;
38
+ display: block;
39
+ box-sizing: border-box;
40
+ width: 100%;
41
+ padding: 4px 0;
42
+ overflow: hidden;
43
+ color: var(--mana-libro-toc-text-color);
44
+ font-weight: 400;
45
+ font-size: 14px;
46
+ line-height: 16px;
47
+ white-space: nowrap;
48
+ text-align: left;
49
+ text-overflow: ellipsis;
50
+ border-left: 2px solid transparent;
51
+ cursor: pointer;
52
+
53
+ &.active {
54
+ font-weight: 600;
55
+ border-left: 2px solid rgba(5, 140, 224, 99.9%);
56
+ }
57
+
58
+ &:hover {
59
+ background-color: var(--mana-libro-toc-hover-color);
60
+ }
61
+
62
+ .libro-toc-collapsed {
63
+ margin-right: 4px;
64
+ }
65
+ }
66
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './module.js';
2
+ export * from './toc-configuration.js';
3
+ export * from './toc-protocol.js';
4
+ export * from './toc-view.js';
5
+ export * from './toc-contribution.js';
@@ -0,0 +1,27 @@
1
+ import type { ColorRegistry } from '@difizen/mana-app';
2
+ import { Color, ColorContribution } from '@difizen/mana-app';
3
+ import { singleton } from '@difizen/mana-app';
4
+
5
+ @singleton({ contrib: ColorContribution })
6
+ export class LibroTocColorRegistry implements ColorContribution {
7
+ registerColors(colors: ColorRegistry): void {
8
+ colors.register(
9
+ // #region antd variable
10
+ {
11
+ id: 'libro.toc.title.color',
12
+ defaults: { dark: '#EDEEEF', light: Color.rgba(0, 0, 0, 0.85) },
13
+ description: '',
14
+ },
15
+ {
16
+ id: 'libro.toc.text.color',
17
+ defaults: { dark: '#E3E4E6', light: Color.rgba(0, 10, 26, 0.68) },
18
+ description: '',
19
+ },
20
+ {
21
+ id: 'libro.toc.hover.color',
22
+ defaults: { dark: '#515359', light: Color.rgba(206, 211, 211, 0.422) },
23
+ description: '',
24
+ },
25
+ );
26
+ }
27
+ }