@difizen/libro-code-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,399 @@
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, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5;
3
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _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; }
7
+ 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); } }
8
+ 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); }); }; }
9
+ 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 }); }
10
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
+ 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); } }
12
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
14
+ 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); }
15
+ function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
16
+ function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
17
+ 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); }
18
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
19
+ 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); }; }
20
+ 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); }
21
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
22
+ 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; } }
23
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
24
+ 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; }
25
+ 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.'); }
26
+ /* eslint-disable react-hooks/exhaustive-deps */
27
+ /* eslint-disable @typescript-eslint/no-parameter-properties */
28
+ /* eslint-disable @typescript-eslint/parameter-properties */
29
+
30
+ import { CodeEditorManager, CodeEditorSettings } from '@difizen/libro-code-editor';
31
+ import { isOutput } from '@difizen/libro-common';
32
+ import { CellService, EditorStatus, LibroExecutableCellView, LibroOutputArea, VirtualizedManagerHelper } from '@difizen/libro-core';
33
+ import { getOrigin, inject, prop, transient, useInject, view, ViewInstance, ViewManager, ViewOption, ViewRender, watch } from '@difizen/mana-app';
34
+ import { Deferred } from '@difizen/mana-app';
35
+ import { useEffect, useMemo, useRef, memo, forwardRef } from 'react';
36
+ import { jsx as _jsx } from "react/jsx-runtime";
37
+ import { Fragment as _Fragment } from "react/jsx-runtime";
38
+ function countLines(inputString) {
39
+ var lines = inputString.split('\n');
40
+ return lines.length;
41
+ }
42
+ var CellEditor = function CellEditor() {
43
+ var _instance$editorView3;
44
+ var instance = useInject(ViewInstance);
45
+ var virtualizedManagerHelper = useInject(VirtualizedManagerHelper);
46
+ var virtualizedManager = virtualizedManagerHelper.getOrCreate(instance.parent.model);
47
+ var editorRef = useRef(null);
48
+ useEffect(function () {
49
+ var _instance$editorView;
50
+ if ((_instance$editorView = instance.editorView) !== null && _instance$editorView !== void 0 && _instance$editorView.editor) {
51
+ var _instance$editorView2;
52
+ instance.editor = getOrigin((_instance$editorView2 = instance.editorView) === null || _instance$editorView2 === void 0 ? void 0 : _instance$editorView2.editor);
53
+ }
54
+ }, [instance, (_instance$editorView3 = instance.editorView) === null || _instance$editorView3 === void 0 ? void 0 : _instance$editorView3.editor]);
55
+ var editorAreaHeight = useMemo(function () {
56
+ return instance.calcEditorAreaHeight();
57
+ }, [instance.model.value]);
58
+ if (virtualizedManager.isVirtualized) {
59
+ instance.renderEditorIntoVirtualized = true;
60
+ if (instance.setEditorHost) {
61
+ instance.setEditorHost(editorRef);
62
+ }
63
+ return /*#__PURE__*/_jsx("div", {
64
+ style: {
65
+ height: "".concat(editorAreaHeight || 0, "px"),
66
+ width: '100%'
67
+ },
68
+ ref: editorRef
69
+ });
70
+ } else {
71
+ return /*#__PURE__*/_jsx(_Fragment, {
72
+ children: instance.editorView && /*#__PURE__*/_jsx(ViewRender, {
73
+ view: instance.editorView
74
+ })
75
+ });
76
+ }
77
+ };
78
+ export var CellEditorMemo = /*#__PURE__*/memo(CellEditor);
79
+ var CodeEditorViewComponent = /*#__PURE__*/forwardRef(function CodeEditorViewComponent(props, ref) {
80
+ var instance = useInject(ViewInstance);
81
+ return /*#__PURE__*/_jsx("div", {
82
+ className: "libro-codemirror-cell-editor",
83
+ ref: ref,
84
+ tabIndex: 10,
85
+ onBlur: instance.blur,
86
+ children: /*#__PURE__*/_jsx(CellEditorMemo, {})
87
+ });
88
+ });
89
+ export var LibroCodeCellView = (_dec = transient(), _dec2 = view('code-editor-cell-view'), _dec3 = prop(), _dec4 = prop(), _dec5 = prop(), _dec6 = prop(), _dec7 = prop(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_LibroExecutableCellV) {
90
+ _inherits(LibroCodeCellView, _LibroExecutableCellV);
91
+ var _super = _createSuper(LibroCodeCellView);
92
+ function LibroCodeCellView(options, cellService, viewManager, codeEditorManager, codeEditorSettings) {
93
+ var _options$cell;
94
+ var _this;
95
+ _classCallCheck(this, LibroCodeCellView);
96
+ _this = _super.call(this, options, cellService);
97
+ _this.view = CodeEditorViewComponent;
98
+ _this.viewManager = void 0;
99
+ _this.codeEditorManager = void 0;
100
+ _this.outputs = void 0;
101
+ _initializerDefineProperty(_this, "editorView", _descriptor, _assertThisInitialized(_this));
102
+ _initializerDefineProperty(_this, "editorAreaHeight", _descriptor2, _assertThisInitialized(_this));
103
+ _initializerDefineProperty(_this, "noEditorAreaHeight", _descriptor3, _assertThisInitialized(_this));
104
+ _initializerDefineProperty(_this, "cellViewTopPos", _descriptor4, _assertThisInitialized(_this));
105
+ _initializerDefineProperty(_this, "editorStatus", _descriptor5, _assertThisInitialized(_this));
106
+ _this.editorViewReadyDeferred = new Deferred();
107
+ _this.outputAreaDeferred = new Deferred();
108
+ _this.codeEditorSettings = void 0;
109
+ _this.renderEditor = function () {
110
+ if (_this.editorView) {
111
+ return /*#__PURE__*/_jsx(ViewRender, {
112
+ view: _this.editorView
113
+ });
114
+ }
115
+ return null;
116
+ };
117
+ _this.blur = function () {
118
+ var _this$editorView, _this$editorView2;
119
+ (_this$editorView = _this.editorView) === null || _this$editorView === void 0 || (_this$editorView = _this$editorView.editor) === null || _this$editorView === void 0 || _this$editorView.setOption('styleActiveLine', false);
120
+ (_this$editorView2 = _this.editorView) === null || _this$editorView2 === void 0 || (_this$editorView2 = _this$editorView2.editor) === null || _this$editorView2 === void 0 || _this$editorView2.setOption('highlightActiveLineGutter', false);
121
+ };
122
+ _this.focus = function (toEdit) {
123
+ if (toEdit) {
124
+ if (_this.parent.model.readOnly === true) {
125
+ return;
126
+ }
127
+ if (!_this.editorView) {
128
+ _this.editorReady.then(function () {
129
+ var _this$editorView3;
130
+ (_this$editorView3 = _this.editorView) === null || _this$editorView3 === void 0 || _this$editorView3.editorReady.then(function () {
131
+ var _this$editorView4, _this$editorView5, _this$editorView6, _this$editorView7;
132
+ (_this$editorView4 = _this.editorView) === null || _this$editorView4 === void 0 || (_this$editorView4 = _this$editorView4.editor) === null || _this$editorView4 === void 0 || _this$editorView4.setOption('styleActiveLine', true);
133
+ (_this$editorView5 = _this.editorView) === null || _this$editorView5 === void 0 || (_this$editorView5 = _this$editorView5.editor) === null || _this$editorView5 === void 0 || _this$editorView5.setOption('highlightActiveLineGutter', true);
134
+ if ((_this$editorView6 = _this.editorView) !== null && _this$editorView6 !== void 0 && (_this$editorView6 = _this$editorView6.editor) !== null && _this$editorView6 !== void 0 && _this$editorView6.hasFocus()) {
135
+ return;
136
+ }
137
+ (_this$editorView7 = _this.editorView) === null || _this$editorView7 === void 0 || (_this$editorView7 = _this$editorView7.editor) === null || _this$editorView7 === void 0 || _this$editorView7.focus();
138
+ return;
139
+ });
140
+ return;
141
+ }).catch(console.error);
142
+ } else {
143
+ var _this$editorView8, _this$editorView9, _this$editorView10, _this$editorView11;
144
+ (_this$editorView8 = _this.editorView) === null || _this$editorView8 === void 0 || (_this$editorView8 = _this$editorView8.editor) === null || _this$editorView8 === void 0 || _this$editorView8.setOption('styleActiveLine', true);
145
+ (_this$editorView9 = _this.editorView) === null || _this$editorView9 === void 0 || (_this$editorView9 = _this$editorView9.editor) === null || _this$editorView9 === void 0 || _this$editorView9.setOption('highlightActiveLineGutter', true);
146
+ if ((_this$editorView10 = _this.editorView) !== null && _this$editorView10 !== void 0 && (_this$editorView10 = _this$editorView10.editor) !== null && _this$editorView10 !== void 0 && _this$editorView10.hasFocus()) {
147
+ return;
148
+ }
149
+ (_this$editorView11 = _this.editorView) === null || _this$editorView11 === void 0 || (_this$editorView11 = _this$editorView11.editor) === null || _this$editorView11 === void 0 || _this$editorView11.focus();
150
+ }
151
+ } else {
152
+ var _this$container, _this$container2;
153
+ 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)) {
154
+ return;
155
+ }
156
+ (_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();
157
+ }
158
+ };
159
+ _this.clearExecution = function () {
160
+ _this.model.clearExecution();
161
+ _this.outputArea.clear();
162
+ };
163
+ _this.options = options;
164
+ _this.viewManager = viewManager;
165
+ _this.codeEditorManager = codeEditorManager;
166
+ _this.codeEditorSettings = codeEditorSettings;
167
+ _this.outputs = (_options$cell = options.cell) === null || _options$cell === void 0 ? void 0 : _options$cell.outputs;
168
+ _this.className = _this.className + ' code';
169
+
170
+ // 创建outputArea
171
+ _this.viewManager.getOrCreateView(LibroOutputArea, {
172
+ cellId: _this.id,
173
+ cell: _assertThisInitialized(_this)
174
+ }).then( /*#__PURE__*/function () {
175
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(outputArea) {
176
+ var output;
177
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
178
+ while (1) switch (_context.prev = _context.next) {
179
+ case 0:
180
+ _this.outputArea = outputArea;
181
+ output = _this.outputs;
182
+ if (!isOutput(output)) {
183
+ _context.next = 5;
184
+ break;
185
+ }
186
+ _context.next = 5;
187
+ return _this.outputArea.fromJSON(output);
188
+ case 5:
189
+ _this.outputAreaDeferred.resolve(outputArea);
190
+ _this.outputWatch();
191
+ return _context.abrupt("return");
192
+ case 8:
193
+ case "end":
194
+ return _context.stop();
195
+ }
196
+ }, _callee);
197
+ }));
198
+ return function (_x) {
199
+ return _ref.apply(this, arguments);
200
+ };
201
+ }()).catch(console.error);
202
+ return _this;
203
+ }
204
+ LibroCodeCellView = inject(CodeEditorSettings)(LibroCodeCellView, undefined, 4) || LibroCodeCellView;
205
+ LibroCodeCellView = inject(CodeEditorManager)(LibroCodeCellView, undefined, 3) || LibroCodeCellView;
206
+ LibroCodeCellView = inject(ViewManager)(LibroCodeCellView, undefined, 2) || LibroCodeCellView;
207
+ LibroCodeCellView = inject(CellService)(LibroCodeCellView, undefined, 1) || LibroCodeCellView;
208
+ LibroCodeCellView = inject(ViewOption)(LibroCodeCellView, undefined, 0) || LibroCodeCellView;
209
+ _createClass(LibroCodeCellView, [{
210
+ key: "editorReady",
211
+ get: function get() {
212
+ return this.editorViewReadyDeferred.promise;
213
+ }
214
+ }, {
215
+ key: "outputAreaReady",
216
+ get: function get() {
217
+ return this.outputAreaDeferred.promise;
218
+ }
219
+ }, {
220
+ key: "calcEditorAreaHeight",
221
+ value:
222
+ // onViewResize(size: ViewSize) {
223
+ // if (size.height) this.editorAreaHeight = size.height;
224
+ // }
225
+ function calcEditorAreaHeight() {
226
+ // if (
227
+ // this.editorStatus === EditorStatus.NOTLOADED ||
228
+ // this.editorStatus === EditorStatus.LOADING
229
+ // ) {
230
+
231
+ var _this$codeEditorManag = this.codeEditorManager.getUserEditorConfig(this.model),
232
+ lineHeight = _this$codeEditorManag.lineHeight,
233
+ paddingTop = _this$codeEditorManag.paddingTop,
234
+ paddingBottom = _this$codeEditorManag.paddingBottom,
235
+ scrollBarHeight = _this$codeEditorManag.scrollBarHeight;
236
+ var codeHeight = countLines(this.model.value) * (lineHeight || 20);
237
+ var editorPadding = paddingTop + paddingBottom;
238
+ var editorAreaHeight = codeHeight + editorPadding + scrollBarHeight;
239
+ this.editorAreaHeight = editorAreaHeight;
240
+
241
+ // 编辑器已经加载的情况下cell高度都由对它的高度监听得到。
242
+ return this.editorAreaHeight;
243
+ }
244
+ }, {
245
+ key: "outputWatch",
246
+ value: function outputWatch() {
247
+ var _this2 = this;
248
+ this.toDispose.push(watch(this.outputArea, 'outputs', function () {
249
+ var _this2$parent$model$o, _this2$parent$model;
250
+ (_this2$parent$model$o = (_this2$parent$model = _this2.parent.model).onChange) === null || _this2$parent$model$o === void 0 || _this2$parent$model$o.call(_this2$parent$model);
251
+ }));
252
+ }
253
+ }, {
254
+ key: "toJSON",
255
+ value: function toJSON() {
256
+ var _this$outputArea$toJS, _this$outputArea;
257
+ var meta = _get(_getPrototypeOf(LibroCodeCellView.prototype), "toJSON", this).call(this);
258
+ return _objectSpread(_objectSpread({}, meta), {}, {
259
+ outputs: (_this$outputArea$toJS = (_this$outputArea = this.outputArea) === null || _this$outputArea === void 0 ? void 0 : _this$outputArea.toJSON()) !== null && _this$outputArea$toJS !== void 0 ? _this$outputArea$toJS : this.outputs
260
+ });
261
+ }
262
+ }, {
263
+ key: "onViewMount",
264
+ value: function onViewMount() {
265
+ var _this$parent$model$ac;
266
+ this.createEditor();
267
+ //选中cell时才focus
268
+ if (((_this$parent$model$ac = this.parent.model.active) === null || _this$parent$model$ac === void 0 ? void 0 : _this$parent$model$ac.id) === this.id) {
269
+ this.focus(!this.parent.model.commandMode);
270
+ }
271
+ }
272
+ }, {
273
+ key: "setEditorHost",
274
+ value: function setEditorHost(ref) {
275
+ var editorHostId = this.parent.id + this.id;
276
+ this.codeEditorManager.setEditorHostRef(editorHostId, ref);
277
+ }
278
+ }, {
279
+ key: "getEditorOption",
280
+ value: function getEditorOption() {
281
+ var option = {
282
+ editorHostId: this.parent.id + this.id,
283
+ model: this.model,
284
+ config: {
285
+ readOnly: this.parent.model.readOnly,
286
+ editable: !this.parent.model.readOnly,
287
+ placeholder: '请输入代码'
288
+ }
289
+ };
290
+ return option;
291
+ }
292
+ }, {
293
+ key: "createEditor",
294
+ value: function () {
295
+ var _createEditor = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
296
+ var option, editorView;
297
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
298
+ while (1) switch (_context2.prev = _context2.next) {
299
+ case 0:
300
+ option = this.getEditorOption();
301
+ this.editorStatus = EditorStatus.LOADING;
302
+
303
+ // 防止虚拟滚动中编辑器被频繁创建
304
+ if (!this.editorView) {
305
+ _context2.next = 5;
306
+ break;
307
+ }
308
+ this.editorStatus = EditorStatus.LOADED;
309
+ return _context2.abrupt("return");
310
+ case 5:
311
+ _context2.next = 7;
312
+ return this.codeEditorManager.getOrCreateEditorView(option);
313
+ case 7:
314
+ editorView = _context2.sent;
315
+ this.editorView = editorView;
316
+ this.editorViewReadyDeferred.resolve();
317
+ this.editorStatus = EditorStatus.LOADED;
318
+ _context2.next = 13;
319
+ return this.afterEditorReady();
320
+ case 13:
321
+ case "end":
322
+ return _context2.stop();
323
+ }
324
+ }, _callee2, this);
325
+ }));
326
+ function createEditor() {
327
+ return _createEditor.apply(this, arguments);
328
+ }
329
+ return createEditor;
330
+ }()
331
+ }, {
332
+ key: "afterEditorReady",
333
+ value: function () {
334
+ var _afterEditorReady = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
335
+ var _this3 = this,
336
+ _this$editorView12;
337
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
338
+ while (1) switch (_context3.prev = _context3.next) {
339
+ case 0:
340
+ watch(this.parent.model, 'readOnly', function () {
341
+ var _this3$editorView;
342
+ (_this3$editorView = _this3.editorView) === null || _this3$editorView === void 0 || (_this3$editorView = _this3$editorView.editor) === null || _this3$editorView === void 0 || _this3$editorView.setOption('readOnly', getOrigin(_this3.parent.model.readOnly));
343
+ });
344
+ (_this$editorView12 = this.editorView) === null || _this$editorView12 === void 0 || _this$editorView12.onModalChange(function (val) {
345
+ return _this3.hasModal = val;
346
+ });
347
+ case 2:
348
+ case "end":
349
+ return _context3.stop();
350
+ }
351
+ }, _callee3, this);
352
+ }));
353
+ function afterEditorReady() {
354
+ return _afterEditorReady.apply(this, arguments);
355
+ }
356
+ return afterEditorReady;
357
+ }()
358
+ }, {
359
+ key: "shouldEnterEditorMode",
360
+ value: function shouldEnterEditorMode(e) {
361
+ var _getOrigin;
362
+ return (_getOrigin = getOrigin(this.editorView)) !== null && _getOrigin !== void 0 && (_getOrigin = _getOrigin.editor) !== null && _getOrigin !== void 0 && (_getOrigin = _getOrigin.host) !== null && _getOrigin !== void 0 && _getOrigin.contains(e.target) ? true : false;
363
+ }
364
+ }]);
365
+ return LibroCodeCellView;
366
+ }(LibroExecutableCellView), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "editorView", [_dec3], {
367
+ configurable: true,
368
+ enumerable: true,
369
+ writable: true,
370
+ initializer: null
371
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "editorAreaHeight", [_dec4], {
372
+ configurable: true,
373
+ enumerable: true,
374
+ writable: true,
375
+ initializer: function initializer() {
376
+ return 0;
377
+ }
378
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "noEditorAreaHeight", [_dec5], {
379
+ configurable: true,
380
+ enumerable: true,
381
+ writable: true,
382
+ initializer: function initializer() {
383
+ return 0;
384
+ }
385
+ }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "cellViewTopPos", [_dec6], {
386
+ configurable: true,
387
+ enumerable: true,
388
+ writable: true,
389
+ initializer: function initializer() {
390
+ return 0;
391
+ }
392
+ }), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "editorStatus", [_dec7], {
393
+ configurable: true,
394
+ enumerable: true,
395
+ writable: true,
396
+ initializer: function initializer() {
397
+ return EditorStatus.NOTLOADED;
398
+ }
399
+ })), _class2)) || _class) || _class);
package/es/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './code-cell-contribution.js';
2
+ export * from './code-cell-model.js';
3
+ export * from './code-cell-protocol.js';
4
+ export * from './code-cell-view.js';
5
+ export * from './module.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC"}
package/es/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./code-cell-contribution.js";
2
+ export * from "./code-cell-model.js";
3
+ export * from "./code-cell-protocol.js";
4
+ export * from "./code-cell-view.js";
5
+ export * from "./module.js";
package/es/module.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { ManaModule } from '@difizen/mana-app';
2
+ export declare const CodeCellModule: ManaModule;
3
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAO/C,eAAO,MAAM,cAAc,YAeE,CAAC"}
package/es/module.js ADDED
@@ -0,0 +1,21 @@
1
+ import { CodeEditorModule } from '@difizen/libro-code-editor';
2
+ import { CellOptions } from '@difizen/libro-core';
3
+ import { ManaModule } from '@difizen/mana-app';
4
+ import { CodeEditorCellContribution } from "./code-cell-contribution.js";
5
+ import { LibroCodeCellModel } from "./code-cell-model.js";
6
+ import { CodeCellModelFactory } from "./code-cell-protocol.js";
7
+ import { LibroCodeCellView } from "./code-cell-view.js";
8
+ export var CodeCellModule = ManaModule.create().register(CodeEditorCellContribution, LibroCodeCellView, LibroCodeCellModel, {
9
+ token: CodeCellModelFactory,
10
+ useFactory: function useFactory(ctx) {
11
+ return function (options) {
12
+ var child = ctx.container.createChild();
13
+ child.register({
14
+ token: CellOptions,
15
+ useValue: options
16
+ });
17
+ var model = child.get(LibroCodeCellModel);
18
+ return model;
19
+ };
20
+ }
21
+ }).dependOn(CodeEditorModule);
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@difizen/libro-code-cell",
3
+ "version": "0.1.2",
4
+ "description": "",
5
+ "keywords": [
6
+ "libro",
7
+ "notebook"
8
+ ],
9
+ "repository": "git@github.com:difizen/libro.git",
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "typings": "./es/index.d.ts",
15
+ "default": "./es/index.js"
16
+ },
17
+ "./mock": {
18
+ "typings": "./es/mock/index.d.ts",
19
+ "default": "./es/mock/index.js"
20
+ },
21
+ "./es/mock": {
22
+ "typings": "./es/mock/index.d.ts",
23
+ "default": "./es/mock/index.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "main": "es/index.js",
28
+ "module": "es/index.js",
29
+ "typings": "es/index.d.ts",
30
+ "files": [
31
+ "es",
32
+ "src"
33
+ ],
34
+ "dependencies": {
35
+ "@difizen/libro-code-editor": "^0.1.2",
36
+ "@difizen/libro-common": "^0.1.2",
37
+ "@difizen/libro-core": "^0.1.2",
38
+ "@difizen/mana-app": "latest"
39
+ },
40
+ "peerDependencies": {
41
+ "react": "^18.2.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/react": "^18.2.25"
45
+ },
46
+ "scripts": {
47
+ "setup": "father build",
48
+ "build": "father build",
49
+ "test": ": Note: lint task is delegated to test:* scripts",
50
+ "test:vitest": "vitest run",
51
+ "test:jest": "jest",
52
+ "coverage": ": Note: lint task is delegated to coverage:* scripts",
53
+ "coverage:vitest": "vitest run --coverage",
54
+ "coverage:jest": "jest --coverage",
55
+ "lint": ": Note: lint task is delegated to lint:* scripts",
56
+ "lint:eslint": "eslint src",
57
+ "lint:tsc": "tsc --noEmit"
58
+ }
59
+ }
@@ -0,0 +1,32 @@
1
+ import { CellModelContribution, CellViewContribution } from '@difizen/libro-core';
2
+ import type { CellMeta, CellModel, CellOptions } from '@difizen/libro-core';
3
+ import { inject, singleton } from '@difizen/mana-app';
4
+
5
+ import { CodeCellModelFactory } from './code-cell-protocol.js';
6
+ import { LibroCodeCellView } from './code-cell-view.js';
7
+
8
+ @singleton({ contrib: [CellModelContribution, CellViewContribution] })
9
+ export class CodeEditorCellContribution
10
+ implements CellModelContribution, CellViewContribution
11
+ {
12
+ @inject(CodeCellModelFactory) libroCellModelFactory: CodeCellModelFactory;
13
+
14
+ cellMeta: CellMeta = {
15
+ type: 'code',
16
+ name: 'Python',
17
+ order: 'b',
18
+ };
19
+
20
+ canHandle(options: CellOptions): number {
21
+ return options?.cell?.cell_type === this.cellMeta.type ? 1000 : 1;
22
+ }
23
+
24
+ async createModel(options: CellOptions): Promise<CellModel> {
25
+ const model = this.libroCellModelFactory(options);
26
+ // await model.outputAreaReady;
27
+ // model.outputArea.setupCellModel(model);
28
+ return model;
29
+ }
30
+
31
+ view = LibroCodeCellView;
32
+ }
@@ -0,0 +1,73 @@
1
+ import type { ExecutionCount } from '@difizen/libro-common';
2
+ import type { ICodeCell } from '@difizen/libro-common';
3
+ import type { ExecutableCellModel } from '@difizen/libro-core';
4
+ import { CellOptions, LibroCellModel } from '@difizen/libro-core';
5
+ import type { Event as ManaEvent } from '@difizen/mana-app';
6
+ import { inject, prop, transient, ViewManager } from '@difizen/mana-app';
7
+ import { Emitter } from '@difizen/mana-app';
8
+
9
+ /**
10
+ * 基础的可执行代码的cell, 带有执行能力
11
+ */
12
+ @transient()
13
+ export class LibroCodeCellModel extends LibroCellModel implements ExecutableCellModel {
14
+ @prop()
15
+ executeCount: ExecutionCount;
16
+ @prop()
17
+ executing: boolean;
18
+ @prop()
19
+ hasOutputHidden: boolean;
20
+ @prop()
21
+ hasOutputsScrolled: boolean;
22
+
23
+ declare libroFormatType: string;
24
+
25
+ viewManager: ViewManager;
26
+
27
+ // Emitter Msg
28
+ msgChangeEmitter: Emitter<any>;
29
+
30
+ get msgChange(): ManaEvent<any> {
31
+ return this.msgChangeEmitter.event;
32
+ }
33
+
34
+ constructor(
35
+ @inject(CellOptions) options: CellOptions,
36
+ @inject(ViewManager) viewManager: ViewManager,
37
+ ) {
38
+ super(options);
39
+ this.executing = false;
40
+ this.msgChangeEmitter = new Emitter<any>();
41
+ this.executeCount = (options.cell as ICodeCell).execution_count || null;
42
+ this.mimeType = 'text/x-python';
43
+ this.hasOutputHidden = false;
44
+ this.hasOutputsScrolled = false;
45
+ this.viewManager = viewManager;
46
+ }
47
+
48
+ override toJSON(): Omit<ICodeCell, 'outputs'> {
49
+ // const outputs = this.outputArea?.toJSON() ?? this.outputs;
50
+ return {
51
+ id: this.id,
52
+ cell_type: this.type,
53
+ source: this.source,
54
+ metadata: this.metadata,
55
+ execution_count: this.executeCount,
56
+ // outputs: this.outputs,
57
+ };
58
+ }
59
+
60
+ clearExecution = () => {
61
+ this.executeCount = null;
62
+ this.executing = false;
63
+ };
64
+
65
+ override dispose() {
66
+ super.dispose();
67
+ this.msgChangeEmitter.dispose();
68
+ }
69
+
70
+ getSource() {
71
+ return this.value;
72
+ }
73
+ }
@@ -0,0 +1,6 @@
1
+ import type { CellOptions } from '@difizen/libro-core';
2
+
3
+ import type { LibroCodeCellModel } from './code-cell-model.js';
4
+
5
+ export type CodeCellModelFactory = (options: CellOptions) => LibroCodeCellModel;
6
+ export const CodeCellModelFactory = Symbol('CodeCellModelFactory');