@difizen/libro-markdown-cell 0.1.2

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.
@@ -0,0 +1,293 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6;
3
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
4
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
5
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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 { CodeEditorManager } from '@difizen/libro-code-editor';
22
+ import { CellService, EditorStatus, LibroEditorCellView } from '@difizen/libro-core';
23
+ import { MarkdownParser } from '@difizen/libro-markdown';
24
+ import { getOrigin, prop, useInject, watch } from '@difizen/mana-app';
25
+ import { view, ViewInstance, ViewManager, ViewOption, ViewRender } from '@difizen/mana-app';
26
+ import { inject, transient } from '@difizen/mana-app';
27
+ import { forwardRef, useEffect } from 'react';
28
+ import "./index.less";
29
+ import { MarkdownPreview } from "./markdown-preview.js";
30
+ import { jsx as _jsx } from "react/jsx-runtime";
31
+ export var MarkdownCell = /*#__PURE__*/forwardRef(function MarkdownCell(props, ref) {
32
+ var _instance$editorView3;
33
+ var instance = useInject(ViewInstance);
34
+ var isEdit = instance.isEdit;
35
+ useEffect(function () {
36
+ var _instance$editorView;
37
+ if ((_instance$editorView = instance.editorView) !== null && _instance$editorView !== void 0 && _instance$editorView.editor) {
38
+ var _instance$editorView2;
39
+ instance.editor = getOrigin((_instance$editorView2 = instance.editorView) === null || _instance$editorView2 === void 0 ? void 0 : _instance$editorView2.editor);
40
+ }
41
+ }, [instance, (_instance$editorView3 = instance.editorView) === null || _instance$editorView3 === void 0 ? void 0 : _instance$editorView3.editor]);
42
+ useEffect(function () {
43
+ if (instance.cellmodel.isEdit) {
44
+ instance.createEditor();
45
+ }
46
+ }, [instance, instance.cellmodel.isEdit]);
47
+ return /*#__PURE__*/_jsx("div", {
48
+ ref: ref,
49
+ tabIndex: 10,
50
+ onBlur: function onBlur(e) {
51
+ var _ref$current;
52
+ if (typeof ref !== 'function' && !(ref !== null && ref !== void 0 && (_ref$current = ref.current) !== null && _ref$current !== void 0 && _ref$current.contains(e.relatedTarget))) {
53
+ instance.blur();
54
+ }
55
+ },
56
+ className: instance.className,
57
+ children: isEdit && instance.editorView ? /*#__PURE__*/_jsx(ViewRender, {
58
+ view: instance.editorView
59
+ }) : /*#__PURE__*/_jsx(MarkdownPreview, {
60
+ instance: instance
61
+ })
62
+ });
63
+ });
64
+ export var MarkdownCellView = (_dec = transient(), _dec2 = view('libro-markdown-cell-view'), _dec3 = prop(), _dec4 = prop(), _dec5 = prop(), _dec6 = prop(), _dec7 = prop(), _dec8 = prop(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_LibroEditorCellView) {
65
+ _inherits(MarkdownCellView, _LibroEditorCellView);
66
+ var _super = _createSuper(MarkdownCellView);
67
+ function MarkdownCellView(options, cellService, viewManager, codeEditorManager, markdownParser) {
68
+ var _this;
69
+ _classCallCheck(this, MarkdownCellView);
70
+ _this = _super.call(this, options, cellService);
71
+ _this.view = MarkdownCell;
72
+ _this.viewManager = void 0;
73
+ _this.codeEditorManager = void 0;
74
+ _initializerDefineProperty(_this, "editorView", _descriptor, _assertThisInitialized(_this));
75
+ _initializerDefineProperty(_this, "editorAreaHeight", _descriptor2, _assertThisInitialized(_this));
76
+ _initializerDefineProperty(_this, "noEditorAreaHeight", _descriptor3, _assertThisInitialized(_this));
77
+ _initializerDefineProperty(_this, "headingCollapsed", _descriptor4, _assertThisInitialized(_this));
78
+ _initializerDefineProperty(_this, "collapsibleChildNumber", _descriptor5, _assertThisInitialized(_this));
79
+ _initializerDefineProperty(_this, "editorStatus", _descriptor6, _assertThisInitialized(_this));
80
+ _this.markdownParser = void 0;
81
+ _this.viewManager = viewManager;
82
+ _this.codeEditorManager = codeEditorManager;
83
+ _this.markdownParser = markdownParser;
84
+ _this.className = _this.className + ' markdown';
85
+ return _this;
86
+ }
87
+ MarkdownCellView = inject(MarkdownParser)(MarkdownCellView, undefined, 4) || MarkdownCellView;
88
+ MarkdownCellView = inject(CodeEditorManager)(MarkdownCellView, undefined, 3) || MarkdownCellView;
89
+ MarkdownCellView = inject(ViewManager)(MarkdownCellView, undefined, 2) || MarkdownCellView;
90
+ MarkdownCellView = inject(CellService)(MarkdownCellView, undefined, 1) || MarkdownCellView;
91
+ MarkdownCellView = inject(ViewOption)(MarkdownCellView, undefined, 0) || MarkdownCellView;
92
+ _createClass(MarkdownCellView, [{
93
+ key: "wrapperCls",
94
+ get: function get() {
95
+ if (!this.cellmodel.isEdit) {
96
+ return 'markdown-cell-preview';
97
+ }
98
+ return '';
99
+ }
100
+ }, {
101
+ key: "isEdit",
102
+ get: function get() {
103
+ var _this$editorView;
104
+ return ((_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.view) && this.cellmodel.isEdit && !this.parent.model.readOnly;
105
+ }
106
+ }, {
107
+ key: "cellmodel",
108
+ get: function get() {
109
+ return this.model;
110
+ }
111
+ }, {
112
+ key: "onViewMount",
113
+ value: function onViewMount() {
114
+ var _this2 = this;
115
+ if (this.cellmodel.isEdit) {
116
+ this.createEditor();
117
+ }
118
+ watch(this.parent.model, 'readOnly', function () {
119
+ if (_this2.parent.model.readOnly === true) {
120
+ _this2.cellmodel.isEdit = false;
121
+ }
122
+ });
123
+ }
124
+ }, {
125
+ key: "onViewResize",
126
+ value: function onViewResize(size) {
127
+ if (size.height) {
128
+ this.editorAreaHeight = size.height;
129
+ }
130
+ }
131
+ }, {
132
+ key: "calcEditorAreaHeight",
133
+ value: function calcEditorAreaHeight() {
134
+ return this.editorAreaHeight;
135
+ }
136
+ }, {
137
+ key: "getEditorOption",
138
+ value: function getEditorOption() {
139
+ var option = {
140
+ model: this.model,
141
+ config: {
142
+ lineNumbers: false,
143
+ foldGutter: false,
144
+ lineWrap: 'on',
145
+ matchBrackets: false,
146
+ autoClosingBrackets: false
147
+ },
148
+ autoFocus: true
149
+ };
150
+ return option;
151
+ }
152
+ }, {
153
+ key: "createEditor",
154
+ value: function () {
155
+ var _createEditor = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
156
+ var option, editorView;
157
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
158
+ while (1) switch (_context.prev = _context.next) {
159
+ case 0:
160
+ option = this.getEditorOption();
161
+ this.editorStatus = EditorStatus.LOADING;
162
+
163
+ // 防止虚拟滚动中编辑器被频繁创建
164
+ if (!this.editorView) {
165
+ _context.next = 5;
166
+ break;
167
+ }
168
+ this.editorStatus = EditorStatus.LOADED;
169
+ return _context.abrupt("return");
170
+ case 5:
171
+ _context.next = 7;
172
+ return this.codeEditorManager.getOrCreateEditorView(option);
173
+ case 7:
174
+ editorView = _context.sent;
175
+ this.editorView = editorView;
176
+ _context.next = 11;
177
+ return editorView.editorReady;
178
+ case 11:
179
+ this.editorStatus = EditorStatus.LOADED;
180
+ _context.next = 14;
181
+ return this.afterEditorReady();
182
+ case 14:
183
+ case "end":
184
+ return _context.stop();
185
+ }
186
+ }, _callee, this);
187
+ }));
188
+ function createEditor() {
189
+ return _createEditor.apply(this, arguments);
190
+ }
191
+ return createEditor;
192
+ }()
193
+ }, {
194
+ key: "afterEditorReady",
195
+ value: function () {
196
+ var _afterEditorReady = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
197
+ var _getOrigin;
198
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
199
+ while (1) switch (_context2.prev = _context2.next) {
200
+ case 0:
201
+ (_getOrigin = getOrigin(this.editorView)) === null || _getOrigin === void 0 || _getOrigin.editor.focus();
202
+ case 1:
203
+ case "end":
204
+ return _context2.stop();
205
+ }
206
+ }, _callee2, this);
207
+ }));
208
+ function afterEditorReady() {
209
+ return _afterEditorReady.apply(this, arguments);
210
+ }
211
+ return afterEditorReady;
212
+ }() // eslint-disable-next-line @typescript-eslint/no-unused-vars
213
+ }, {
214
+ key: "shouldEnterEditorMode",
215
+ value: function shouldEnterEditorMode(e) {
216
+ if (!this.cellmodel.isEdit) {
217
+ return false;
218
+ }
219
+ return true;
220
+ }
221
+ }, {
222
+ key: "focus",
223
+ value: function focus(toEdit) {
224
+ if (toEdit) {
225
+ this.cellmodel.isEdit = true;
226
+ } else {
227
+ var _this$container, _this$container2;
228
+ if ((_this$container = this.container) !== null && _this$container !== void 0 && (_this$container = _this$container.current) !== null && _this$container !== void 0 && (_this$container = _this$container.parentElement) !== null && _this$container !== void 0 && _this$container.contains(document.activeElement)) {
229
+ return;
230
+ }
231
+ (_this$container2 = this.container) === null || _this$container2 === void 0 || (_this$container2 = _this$container2.current) === null || _this$container2 === void 0 || (_this$container2 = _this$container2.parentElement) === null || _this$container2 === void 0 || _this$container2.focus();
232
+ }
233
+ }
234
+ }, {
235
+ key: "blur",
236
+ value: function blur() {
237
+ this.cellmodel.isEdit = false;
238
+ }
239
+ }, {
240
+ key: "redo",
241
+ value: function redo() {
242
+ // this.editor?.trigger('', 'redo', '');
243
+ // this.editor?.focus();
244
+ }
245
+ }, {
246
+ key: "undo",
247
+ value: function undo() {
248
+ // this.editor?.trigger('undo', 'undo', {});
249
+ // this.editor?.focus();
250
+ }
251
+ }]);
252
+ return MarkdownCellView;
253
+ }(LibroEditorCellView), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "editorView", [_dec3], {
254
+ configurable: true,
255
+ enumerable: true,
256
+ writable: true,
257
+ initializer: null
258
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "editorAreaHeight", [_dec4], {
259
+ configurable: true,
260
+ enumerable: true,
261
+ writable: true,
262
+ initializer: function initializer() {
263
+ return 0;
264
+ }
265
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "noEditorAreaHeight", [_dec5], {
266
+ configurable: true,
267
+ enumerable: true,
268
+ writable: true,
269
+ initializer: function initializer() {
270
+ return 0;
271
+ }
272
+ }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "headingCollapsed", [_dec6], {
273
+ configurable: true,
274
+ enumerable: true,
275
+ writable: true,
276
+ initializer: function initializer() {
277
+ return false;
278
+ }
279
+ }), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "collapsibleChildNumber", [_dec7], {
280
+ configurable: true,
281
+ enumerable: true,
282
+ writable: true,
283
+ initializer: function initializer() {
284
+ return 0;
285
+ }
286
+ }), _descriptor6 = _applyDecoratedDescriptor(_class2.prototype, "editorStatus", [_dec8], {
287
+ configurable: true,
288
+ enumerable: true,
289
+ writable: true,
290
+ initializer: function initializer() {
291
+ return EditorStatus.NOTLOADED;
292
+ }
293
+ })), _class2)) || _class) || _class);
@@ -0,0 +1,7 @@
1
+ import type { FC } from 'react';
2
+ import './index.less';
3
+ import type { MarkdownCellView } from './markdown-cell-view.js';
4
+ export declare const MarkdownPreview: FC<{
5
+ instance: MarkdownCellView;
6
+ }>;
7
+ //# sourceMappingURL=markdown-preview.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown-preview.d.ts","sourceRoot":"","sources":["../src/markdown-preview.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC;AAGhC,OAAO,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,eAAO,MAAM,eAAe,EAAE,EAAE,CAAC;IAAE,QAAQ,EAAE,gBAAgB,CAAA;CAAE,CA4B9D,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+ import "./index.less";
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ export var MarkdownPreview = function MarkdownPreview(_ref) {
5
+ var instance = _ref.instance;
6
+ var mktRef = useRef(null);
7
+ // const instance = useInject<MarkdownCellView>(ViewInstance);
8
+
9
+ var enterEdit = useCallback(function (e) {
10
+ if (instance.parent.model.readOnly) {
11
+ return;
12
+ }
13
+ e.preventDefault();
14
+ instance.focus(true);
15
+ }, [instance]);
16
+ useEffect(function () {
17
+ if (mktRef.current) {
18
+ mktRef.current.innerHTML = instance.markdownParser.render(instance.model.value, {
19
+ cellId: instance.model.id
20
+ });
21
+ }
22
+ }, [instance, instance.model.value]);
23
+ return /*#__PURE__*/_jsx("div", {
24
+ className: "libro-markdown-warpper-container",
25
+ onDoubleClick: enterEdit,
26
+ children: /*#__PURE__*/_jsx("div", {
27
+ className: "libro-markdown-preview",
28
+ ref: mktRef
29
+ })
30
+ });
31
+ };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@difizen/libro-markdown-cell",
3
+ "version": "0.1.2",
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
+ "peerDependencies": {
36
+ "react": "^18.2.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/react": "^18.2.25"
40
+ },
41
+ "dependencies": {
42
+ "@difizen/libro-core": "^0.1.2",
43
+ "@difizen/libro-common": "^0.1.2",
44
+ "@difizen/libro-code-editor": "^0.1.2",
45
+ "@difizen/libro-markdown": "^0.1.2",
46
+ "@difizen/mana-app": "latest",
47
+ "@types/markdown-it": "^12.2.3",
48
+ "markdown-it": "^13.0.1",
49
+ "markdown-it-anchor": "^8.6.5"
50
+ },
51
+ "scripts": {
52
+ "setup": "father build",
53
+ "build": "father build",
54
+ "test": ": Note: lint task is delegated to test:* scripts",
55
+ "test:vitest": "vitest run",
56
+ "test:jest": "jest",
57
+ "coverage": ": Note: lint task is delegated to coverage:* scripts",
58
+ "coverage:vitest": "vitest run --coverage",
59
+ "coverage:jest": "jest --coverage",
60
+ "lint": ": Note: lint task is delegated to lint:* scripts",
61
+ "lint:eslint": "eslint src",
62
+ "lint:tsc": "tsc --noEmit"
63
+ }
64
+ }
package/src/index.less ADDED
@@ -0,0 +1,116 @@
1
+ .libro-dnd-cell-container {
2
+ .markdown-cell-preview.libro-dnd-cell {
3
+ border: 1px solid transparent;
4
+ box-shadow: unset;
5
+
6
+ &.active.command-mode {
7
+ border: 1px solid rgb(55, 147, 239);
8
+ box-shadow: unset;
9
+ }
10
+
11
+ .libro-dnd-cell-preview {
12
+ display: block;
13
+ }
14
+
15
+ .libro-markdown-warpper-container {
16
+ .libro-markdown-preview {
17
+ min-height: 50px;
18
+ padding: 0 4px;
19
+ overflow: hidden;
20
+
21
+ img {
22
+ max-width: 100%;
23
+ max-height: 100%;
24
+ }
25
+
26
+ a {
27
+ text-decoration: none;
28
+ }
29
+
30
+ a:hover {
31
+ text-decoration: underline;
32
+ }
33
+
34
+ a:focus,
35
+ input:focus,
36
+ select:focus,
37
+ textarea:focus {
38
+ outline: 1px solid -webkit-focus-ring-color;
39
+ outline-offset: -1px;
40
+ }
41
+
42
+ hr {
43
+ height: 2px;
44
+ border: 0;
45
+ border-bottom: 2px solid;
46
+ }
47
+
48
+ div {
49
+ width: 100%;
50
+ min-height: 20px;
51
+ }
52
+
53
+ /* Adjust margin of first item in markdown cell */
54
+ *:first-child {
55
+ margin-top: 0;
56
+ }
57
+
58
+ /* h1 tags don't need top margin */
59
+ h1:first-child {
60
+ margin-top: 0;
61
+ }
62
+
63
+ /* Removes bottom margin when only one item exists in markdown cell */
64
+ #preview > *:only-child,
65
+ #preview > *:last-child {
66
+ margin-bottom: 0;
67
+ padding-bottom: 0;
68
+ }
69
+
70
+ table {
71
+ border-collapse: collapse;
72
+ border-spacing: 0;
73
+ }
74
+
75
+ table th,
76
+ table td {
77
+ border: 1px solid;
78
+ }
79
+
80
+ table > thead > tr > th {
81
+ text-align: left;
82
+ border-bottom: 1px solid;
83
+ }
84
+
85
+ table > thead > tr > th,
86
+ table > thead > tr > td,
87
+ table > tbody > tr > th,
88
+ table > tbody > tr > td {
89
+ padding: 5px 10px;
90
+ }
91
+
92
+ table > tbody > tr + tr > td {
93
+ border-top: 1px solid;
94
+ }
95
+
96
+ blockquote {
97
+ margin: 0 7px 0 5px;
98
+ padding: 0 16px 0 10px;
99
+ border-left-width: 5px;
100
+ border-left-style: solid;
101
+ }
102
+
103
+ code {
104
+ font-size: 1em;
105
+ }
106
+
107
+ pre code {
108
+ color: var(--mana-libro-text-default-color);
109
+ font-family: 12px;
110
+ line-height: 1.357em;
111
+ white-space: pre-wrap;
112
+ }
113
+ }
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,10 @@
1
+ import 'reflect-metadata';
2
+ import assert from 'assert';
3
+
4
+ import { MarkdownCellModule } from './index.js';
5
+
6
+ describe('libro-markdown-cell', () => {
7
+ it('#import', () => {
8
+ assert(MarkdownCellModule);
9
+ });
10
+ });
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './markdown-cell-contribution.js';
2
+ export * from './markdown-cell-model.js';
3
+ export * from './markdown-cell-module.js';
4
+ export * from './markdown-cell-protocol.js';
5
+ export * from './markdown-cell-view.js';
6
+ export * from './markdown-preview.js';
@@ -0,0 +1,33 @@
1
+ import type { CellMeta, CellOptions, CellModel } from '@difizen/libro-core';
2
+ import { CellModelContribution, CellViewContribution } from '@difizen/libro-core';
3
+ import { singleton, inject } from '@difizen/mana-app';
4
+
5
+ import { MarkdownCellModelFactory } from './markdown-cell-protocol.js';
6
+ import { MarkdownCellView } from './markdown-cell-view.js';
7
+
8
+ @singleton({ contrib: [CellModelContribution, CellViewContribution] })
9
+ export class MarkdownCellContribution
10
+ implements CellModelContribution, CellViewContribution
11
+ {
12
+ @inject(MarkdownCellModelFactory) markdownCellModelFactory: MarkdownCellModelFactory;
13
+
14
+ cellMeta: CellMeta = {
15
+ type: 'markdown',
16
+ name: 'Markdown',
17
+ order: 'c',
18
+ };
19
+
20
+ canHandle(options: CellOptions): number {
21
+ if (options?.cell?.cell_type === this.cellMeta.type) {
22
+ return 999;
23
+ }
24
+ return -1;
25
+ }
26
+
27
+ createModel(options: CellOptions): CellModel {
28
+ const model = this.markdownCellModelFactory(options);
29
+ return model;
30
+ }
31
+
32
+ view = MarkdownCellView;
33
+ }
@@ -0,0 +1,40 @@
1
+ import type { IMarkdownCell } from '@difizen/libro-common';
2
+ import { concatMultilineString } from '@difizen/libro-common';
3
+ import type { LibroMarkdownCellModel } from '@difizen/libro-core';
4
+ import { CellOptions, LibroCellModel } from '@difizen/libro-core';
5
+ import { prop } from '@difizen/mana-app';
6
+ import { inject, transient } from '@difizen/mana-app';
7
+
8
+ @transient()
9
+ export class MarkdownCellModel
10
+ extends LibroCellModel
11
+ implements LibroMarkdownCellModel
12
+ {
13
+ @prop()
14
+ override mimeType = 'text/x-markdown';
15
+
16
+ constructor(@inject(CellOptions) options: CellOptions) {
17
+ super(options);
18
+ if (options.cell?.id) {
19
+ this.id = options.cell?.id as string;
20
+ }
21
+ if (options.cell?.source) {
22
+ this.value = concatMultilineString(options.cell?.source);
23
+ }
24
+ }
25
+
26
+ @prop()
27
+ isEdit = false;
28
+
29
+ @prop()
30
+ preview = '';
31
+
32
+ override toJSON(): IMarkdownCell {
33
+ return {
34
+ id: this.id,
35
+ cell_type: 'markdown',
36
+ source: this.source,
37
+ metadata: this.metadata,
38
+ };
39
+ }
40
+ }
@@ -0,0 +1,26 @@
1
+ import { CellOptions } from '@difizen/libro-core';
2
+ import { MarkdownModule } from '@difizen/libro-markdown';
3
+ import { ManaModule } from '@difizen/mana-app';
4
+
5
+ import { MarkdownCellContribution } from './markdown-cell-contribution.js';
6
+ import { MarkdownCellModel } from './markdown-cell-model.js';
7
+ import { MarkdownCellModelFactory } from './markdown-cell-protocol.js';
8
+ import { MarkdownCellView } from './markdown-cell-view.js';
9
+
10
+ export const MarkdownCellModule = ManaModule.create()
11
+ .register(MarkdownCellContribution, MarkdownCellView, MarkdownCellModel)
12
+ .register({
13
+ token: MarkdownCellModelFactory,
14
+ useFactory: (ctx) => {
15
+ return (options: CellOptions) => {
16
+ const child = ctx.container.createChild();
17
+ child.register({
18
+ token: CellOptions,
19
+ useValue: options,
20
+ });
21
+ const model = child.get(MarkdownCellModel);
22
+ return model;
23
+ };
24
+ },
25
+ })
26
+ .dependOn(MarkdownModule);
@@ -0,0 +1,6 @@
1
+ import type { CellOptions } from '@difizen/libro-core';
2
+
3
+ import type { MarkdownCellModel } from './markdown-cell-model.js';
4
+
5
+ export type MarkdownCellModelFactory = (options: CellOptions) => MarkdownCellModel;
6
+ export const MarkdownCellModelFactory = Symbol('MarkdownCellModelFactory');