@miso.ai/client-sdk-ui 1.9.10-beta.0 → 1.10.0-beta.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.
@@ -75,73 +75,28 @@ class TypewriterLayout extends _progressive.default {
75
75
  const context = await this._view._views._extensions.require('markdown');
76
76
  const cursorClass = (0, _utils.cursorClassName)(this.className);
77
77
  const {
78
- onDebug
78
+ onDebug,
79
+ onCitationLink
79
80
  } = this.options;
80
81
  // TODO: options
81
82
  this._renderer = context.createRenderer({
82
- onRefChange: (oldRef, newRef) => {
83
- oldRef && oldRef.classList.remove(cursorClass);
84
- newRef && newRef.classList.add(cursorClass);
85
- },
86
- onDone: element => {
87
- element.classList.add('done');
88
- },
89
- onDebug,
90
- onCitationLink: this._handleCitationLink.bind(this)
83
+ cursorClass,
84
+ getSource: index => this._getSource(index),
85
+ onCitationLink,
86
+ onDebug
91
87
  });
92
88
  // capture citation link click if necessary
93
89
  this._unsubscribes.push(this._view.proxyElement.on('click', e => this._handleClick(e)));
94
90
  }
95
- _handleCitationLink(methods, index) {
96
- const {
97
- onCitationLink
98
- } = this.options;
99
- if (typeof onCitationLink !== 'function') {
100
- return;
101
- }
91
+ _getSource(index) {
102
92
  const {
103
93
  data = {}
104
94
  } = this._view._data;
105
95
  const {
106
96
  sources = []
107
97
  } = data;
108
- const source = sources[index];
109
- try {
110
- onCitationLink(methods, {
111
- source,
112
- index
113
- });
114
- } catch (e) {
115
- console.error(e);
116
- }
98
+ return sources[index];
117
99
  }
118
-
119
- /*
120
- _handleCitationTooltip({ node, tooltip, index }) {
121
- const { tooltip: tooltipOptions } = this.options;
122
- if (!tooltipOptions || !tooltip || tooltip.properties['data-value']) {
123
- return;
124
- }
125
- const { data = {} } = this._view._data;
126
- const { sources = [] } = data;
127
- const source = sources[index];
128
- if (!source) {
129
- return;
130
- }
131
- // TODO: allow HTML
132
- let value;
133
- try {
134
- value = typeof tooltipOptions === 'function' ? tooltipOptions(source) : source.title;
135
- } catch (e) {
136
- console.error(e);
137
- }
138
- if (!value) {
139
- return;
140
- }
141
- tooltip.properties['data-value'] = escapeHtml(value);
142
- }
143
- */
144
-
145
100
  async _setupForPlaintext() {
146
101
  this._renderer = new _plaintext.default();
147
102
  }
@@ -3,95 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.loadStyles = loadStyles;
7
6
  exports.loadStylesIfNecessary = loadStylesIfNecessary;
8
- var _version = _interopRequireDefault(require("./version.js"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const {
11
- currentScript
12
- } = document;
7
+ var _commons = require("@miso.ai/commons");
8
+ const NAME = 'ui';
13
9
  let cssUrl;
14
10
  let stylesLoaded;
15
11
  async function loadStylesIfNecessary() {
16
- return stylesLoaded || (stylesLoaded = loadStyles());
17
- }
18
- async function loadStyles() {
19
- try {
20
- let link = getExistedLinkElement();
21
- if (!link) {
22
- link = document.createElement('link');
23
- link.rel = 'stylesheet';
24
- link.href = getCssUrl();
25
- document.head.appendChild(link);
26
- }
27
- return waitForStylesApplied(link);
28
- } catch (e) {
29
- console.error(e);
30
- }
12
+ return stylesLoaded || (stylesLoaded = (0, _commons.loadStyles)(getCssUrl()));
31
13
  }
32
14
 
33
15
  // helpers //
34
16
  function getCssUrl() {
35
- return cssUrl || (cssUrl = resolveCssUrl());
36
- }
37
- function resolveCssUrl() {
38
- // TODO: refactor with getPluginScriptUrl() in core
39
- if (_version.default === 'dev') {
40
- const src = currentScript.src;
41
- const searchIndex = src.indexOf('?');
42
- const k = searchIndex < 0 ? src.length : searchIndex;
43
- const i = src.lastIndexOf('/', k); // .../dist/umd/*.min.js
44
- return `${src.slice(0, i - 4)}/css/ui.css`;
45
- } else {
46
- return `https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk@${_version.default}/dist/css/ui.css`;
47
- }
48
- }
49
- function getExistedLinkElement() {
50
- return document.head.querySelector(`link[href="${getCssUrl()}"]`);
51
- }
52
- async function waitForStylesApplied(element) {
53
- await waitForSheetAvailable(element);
54
- await waitForSheetIncluded(element);
55
- }
56
- async function waitForSheetAvailable(element) {
57
- if (element.sheet) {
58
- return;
59
- }
60
- return new Promise((resolve, reject) => {
61
- let intervalId;
62
- element.onerror = () => {
63
- intervalId && clearInterval(intervalId);
64
- reject();
65
- };
66
- intervalId = setInterval(() => {
67
- if (!element.sheet) {
68
- return;
69
- }
70
- intervalId && clearInterval(intervalId);
71
- resolve();
72
- }, 100);
73
- });
74
- }
75
- async function waitForSheetIncluded(element) {
76
- if (isSheetIncluded(element)) {
77
- return;
78
- }
79
- return new Promise(resolve => {
80
- let intervalId;
81
- intervalId = setInterval(() => {
82
- if (!isSheetIncluded(element)) {
83
- return;
84
- }
85
- intervalId && clearInterval(intervalId);
86
- resolve();
87
- }, 100);
88
- });
89
- }
90
- function isSheetIncluded(element) {
91
- for (const sheet of document.styleSheets) {
92
- if (sheet.ownerNode === element) {
93
- return true;
94
- }
95
- }
96
- return false;
17
+ return cssUrl || (cssUrl = (0, _commons.resolveCssUrl)(NAME));
97
18
  }
@@ -4,4 +4,4 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = exports.default = '1.9.10-beta.0';
7
+ var _default = exports.default = '1.10.0-beta.0';
package/package.json CHANGED
@@ -26,7 +26,7 @@
26
26
  "url": "https://github.com/MisoAI/miso-client-js-sdk/issues"
27
27
  },
28
28
  "dependencies": {
29
- "@miso.ai/commons": "1.9.10-beta.0"
29
+ "@miso.ai/commons": "1.10.0-beta.0"
30
30
  },
31
- "version": "1.9.10-beta.0"
31
+ "version": "1.10.0-beta.0"
32
32
  }
@@ -72,63 +72,23 @@ export default class TypewriterLayout extends ProgressiveLayout {
72
72
  async _setupForMarkdown() {
73
73
  const context = await this._view._views._extensions.require('markdown');
74
74
  const cursorClass = cursorClassName(this.className);
75
- const { onDebug } = this.options;
75
+ const { onDebug, onCitationLink } = this.options;
76
76
  // TODO: options
77
77
  this._renderer = context.createRenderer({
78
- onRefChange: (oldRef, newRef) => {
79
- oldRef && oldRef.classList.remove(cursorClass);
80
- newRef && newRef.classList.add(cursorClass);
81
- },
82
- onDone: (element) => {
83
- element.classList.add('done');
84
- },
78
+ cursorClass,
79
+ getSource: index => this._getSource(index),
80
+ onCitationLink,
85
81
  onDebug,
86
- onCitationLink: this._handleCitationLink.bind(this),
87
82
  });
88
83
  // capture citation link click if necessary
89
84
  this._unsubscribes.push(this._view.proxyElement.on('click', (e) => this._handleClick(e)));
90
85
  }
91
86
 
92
- _handleCitationLink(methods, index) {
93
- const { onCitationLink } = this.options;
94
- if (typeof onCitationLink !== 'function') {
95
- return;
96
- }
97
- const { data = {} } = this._view._data;
98
- const { sources = [] } = data;
99
- const source = sources[index];
100
- try {
101
- onCitationLink(methods, { source, index });
102
- } catch (e) {
103
- console.error(e);
104
- }
105
- }
106
-
107
- /*
108
- _handleCitationTooltip({ node, tooltip, index }) {
109
- const { tooltip: tooltipOptions } = this.options;
110
- if (!tooltipOptions || !tooltip || tooltip.properties['data-value']) {
111
- return;
112
- }
87
+ _getSource(index) {
113
88
  const { data = {} } = this._view._data;
114
89
  const { sources = [] } = data;
115
- const source = sources[index];
116
- if (!source) {
117
- return;
118
- }
119
- // TODO: allow HTML
120
- let value;
121
- try {
122
- value = typeof tooltipOptions === 'function' ? tooltipOptions(source) : source.title;
123
- } catch (e) {
124
- console.error(e);
125
- }
126
- if (!value) {
127
- return;
128
- }
129
- tooltip.properties['data-value'] = escapeHtml(value);
90
+ return sources[index];
130
91
  }
131
- */
132
92
 
133
93
  async _setupForPlaintext() {
134
94
  this._renderer = new PlaintextRenderer();
package/src/styles.js CHANGED
@@ -1,97 +1,15 @@
1
- import version from './version.js';
1
+ import { loadStyles, resolveCssUrl } from '@miso.ai/commons';
2
2
 
3
- const { currentScript } = document;
3
+ const NAME = 'ui';
4
4
 
5
5
  let cssUrl;
6
6
  let stylesLoaded;
7
7
 
8
8
  export async function loadStylesIfNecessary() {
9
- return stylesLoaded || (stylesLoaded = loadStyles());
10
- }
11
-
12
- export async function loadStyles() {
13
- try {
14
- let link = getExistedLinkElement();
15
- if (!link) {
16
- link = document.createElement('link');
17
- link.rel = 'stylesheet';
18
- link.href = getCssUrl();
19
- document.head.appendChild(link);
20
- }
21
- return waitForStylesApplied(link);
22
- } catch(e) {
23
- console.error(e);
24
- }
9
+ return stylesLoaded || (stylesLoaded = loadStyles(getCssUrl()));
25
10
  }
26
11
 
27
12
  // helpers //
28
13
  function getCssUrl() {
29
- return cssUrl || (cssUrl = resolveCssUrl());
30
- }
31
-
32
- function resolveCssUrl() {
33
- // TODO: refactor with getPluginScriptUrl() in core
34
- if (version === 'dev') {
35
- const src = currentScript.src;
36
- const searchIndex = src.indexOf('?');
37
- const k = searchIndex < 0 ? src.length : searchIndex;
38
- const i = src.lastIndexOf('/', k); // .../dist/umd/*.min.js
39
- return `${src.slice(0, i - 4)}/css/ui.css`;
40
- } else {
41
- return `https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk@${version}/dist/css/ui.css`;
42
- }
43
- }
44
-
45
- function getExistedLinkElement() {
46
- return document.head.querySelector(`link[href="${getCssUrl()}"]`);
47
- }
48
-
49
- async function waitForStylesApplied(element) {
50
- await waitForSheetAvailable(element);
51
- await waitForSheetIncluded(element);
52
- }
53
-
54
- async function waitForSheetAvailable(element) {
55
- if (element.sheet) {
56
- return;
57
- }
58
- return new Promise((resolve, reject) => {
59
- let intervalId;
60
- element.onerror = () => {
61
- intervalId && clearInterval(intervalId);
62
- reject();
63
- };
64
- intervalId = setInterval(() => {
65
- if (!element.sheet) {
66
- return;
67
- }
68
- intervalId && clearInterval(intervalId);
69
- resolve();
70
- }, 100);
71
- });
72
- }
73
-
74
- async function waitForSheetIncluded(element) {
75
- if (isSheetIncluded(element)) {
76
- return;
77
- }
78
- return new Promise((resolve) => {
79
- let intervalId;
80
- intervalId = setInterval(() => {
81
- if (!isSheetIncluded(element)) {
82
- return;
83
- }
84
- intervalId && clearInterval(intervalId);
85
- resolve();
86
- }, 100);
87
- });
88
- }
89
-
90
- function isSheetIncluded(element) {
91
- for (const sheet of document.styleSheets) {
92
- if (sheet.ownerNode === element) {
93
- return true;
94
- }
95
- }
96
- return false;
14
+ return cssUrl || (cssUrl = resolveCssUrl(NAME));
97
15
  }
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export default '1.9.10-beta.0';
1
+ export default '1.10.0-beta.0';