2
0

view-sidebar.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. /* jshint esversion: 6 */
  2. var sidebar = sidebar || {};
  3. var base = base || require('./base');
  4. sidebar.Sidebar = class {
  5. constructor(host, id) {
  6. this._host = host;
  7. this._id = id ? ('-' + id) : '';
  8. this._stack = [];
  9. this._closeSidebarHandler = () => {
  10. this._pop();
  11. };
  12. this._closeSidebarKeyDownHandler = (e) => {
  13. if (e.keyCode == 27) {
  14. e.preventDefault();
  15. this._pop();
  16. }
  17. };
  18. }
  19. _getElementById(id) {
  20. return this._host.document.getElementById(id + this._id);
  21. }
  22. open(content, title) {
  23. this.close();
  24. this.push(content, title);
  25. }
  26. close() {
  27. this._deactivate();
  28. this._stack = [];
  29. this._hide();
  30. }
  31. push(content, title) {
  32. const item = { title: title, content: content };
  33. this._stack.push(item);
  34. this._activate(item);
  35. }
  36. _pop() {
  37. this._deactivate();
  38. if (this._stack.length > 0) {
  39. this._stack.pop();
  40. }
  41. if (this._stack.length > 0) {
  42. this._activate(this._stack[this._stack.length - 1]);
  43. }
  44. else {
  45. this._hide();
  46. }
  47. }
  48. _hide() {
  49. const sidebarElement = this._getElementById('sidebar');
  50. if (sidebarElement) {
  51. sidebarElement.style.width = '0';
  52. }
  53. const graphElement = this._getElementById('graph');
  54. if (graphElement) {
  55. graphElement.style.marginRight = '0';
  56. }
  57. }
  58. _deactivate() {
  59. const sidebarElement = this._getElementById('sidebar');
  60. if (sidebarElement) {
  61. const closeButton = this._getElementById('sidebar-closebutton');
  62. if (closeButton) {
  63. closeButton.removeEventListener('click', this._closeSidebarHandler);
  64. closeButton.style.color = '#f8f8f8';
  65. }
  66. this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler);
  67. }
  68. }
  69. _activate(item) {
  70. const width = 'min(calc(100vw * 0.6), 500px)';
  71. const sidebarElement = this._getElementById('sidebar');
  72. if (sidebarElement) {
  73. sidebarElement.innerHTML = '';
  74. const titleElement = this._host.document.createElement('h1');
  75. titleElement.classList.add('sidebar-title');
  76. titleElement.innerHTML = item.title ? item.title.toUpperCase() : '';
  77. sidebarElement.appendChild(titleElement);
  78. const closeButton = this._host.document.createElement('a');
  79. closeButton.classList.add('sidebar-closebutton');
  80. closeButton.setAttribute('id', 'sidebar-closebutton');
  81. closeButton.setAttribute('href', 'javascript:void(0)');
  82. closeButton.innerHTML = '×';
  83. closeButton.addEventListener('click', this._closeSidebarHandler);
  84. sidebarElement.appendChild(closeButton);
  85. const contentElement = this._host.document.createElement('div');
  86. contentElement.classList.add('sidebar-content');
  87. contentElement.setAttribute('id', 'sidebar-content');
  88. sidebarElement.appendChild(contentElement);
  89. if (typeof content == 'string') {
  90. contentElement.innerHTML = item.content;
  91. }
  92. else if (item.content instanceof Array) {
  93. for (const element of item.content) {
  94. contentElement.appendChild(element);
  95. }
  96. }
  97. else {
  98. contentElement.appendChild(item.content);
  99. }
  100. sidebarElement.style.width = width;
  101. this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler);
  102. }
  103. const graphElement = this._getElementById('graph');
  104. if (graphElement) {
  105. graphElement.style.marginRight = width;
  106. }
  107. }
  108. };
  109. sidebar.NodeSidebar = class {
  110. constructor(host, node) {
  111. this._host = host;
  112. this._node = node;
  113. this._elements = [];
  114. this._attributes = [];
  115. this._inputs = [];
  116. this._outputs = [];
  117. if (node.type) {
  118. let showDocumentation = null;
  119. const type = node.type;
  120. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  121. showDocumentation = {};
  122. showDocumentation.text = type.nodes ? '\u0192': '?';
  123. showDocumentation.callback = () => {
  124. this._raise('show-documentation', null);
  125. };
  126. }
  127. this._addProperty('type', new sidebar.ValueTextView(this._host, node.type.name, showDocumentation));
  128. if (node.type.module) {
  129. this._addProperty('module', new sidebar.ValueTextView(this._host, node.type.module));
  130. }
  131. }
  132. if (node.name) {
  133. this._addProperty('name', new sidebar.ValueTextView(this._host, node.name));
  134. }
  135. if (node.location) {
  136. this._addProperty('location', new sidebar.ValueTextView(this._host, node.location));
  137. }
  138. if (node.description) {
  139. this._addProperty('description', new sidebar.ValueTextView(this._host, node.description));
  140. }
  141. if (node.device) {
  142. this._addProperty('device', new sidebar.ValueTextView(this._host, node.device));
  143. }
  144. const attributes = node.attributes;
  145. if (attributes && attributes.length > 0) {
  146. const sortedAttributes = node.attributes.slice();
  147. sortedAttributes.sort((a, b) => {
  148. const au = a.name.toUpperCase();
  149. const bu = b.name.toUpperCase();
  150. return (au < bu) ? -1 : (au > bu) ? 1 : 0;
  151. });
  152. this._addHeader('Attributes');
  153. for (const attribute of sortedAttributes) {
  154. this._addAttribute(attribute.name, attribute);
  155. }
  156. }
  157. const inputs = node.inputs;
  158. if (inputs && inputs.length > 0) {
  159. this._addHeader('Inputs');
  160. for (const input of inputs) {
  161. this._addInput(input.name, input);
  162. }
  163. }
  164. const outputs = node.outputs;
  165. if (outputs && outputs.length > 0) {
  166. this._addHeader('Outputs');
  167. for (const output of outputs) {
  168. this._addOutput(output.name, output);
  169. }
  170. }
  171. const separator = this._host.document.createElement('div');
  172. separator.className = 'sidebar-view-separator';
  173. this._elements.push(separator);
  174. }
  175. render() {
  176. return this._elements;
  177. }
  178. _addHeader(title) {
  179. const headerElement = this._host.document.createElement('div');
  180. headerElement.className = 'sidebar-view-header';
  181. headerElement.innerText = title;
  182. this._elements.push(headerElement);
  183. }
  184. _addProperty(name, value) {
  185. const item = new sidebar.NameValueView(this._host, name, value);
  186. this._elements.push(item.render());
  187. }
  188. _addAttribute(name, attribute) {
  189. const item = new NodeAttributeView(this._host, attribute);
  190. item.on('show-graph', (sender, graph) => {
  191. this._raise('show-graph', graph);
  192. });
  193. const view = new sidebar.NameValueView(this._host, name, item);
  194. this._attributes.push(view);
  195. this._elements.push(view.render());
  196. }
  197. _addInput(name, input) {
  198. if (input.arguments.length > 0) {
  199. const view = new sidebar.ParameterView(this._host, input);
  200. view.on('export-tensor', (sender, tensor) => {
  201. this._raise('export-tensor', tensor);
  202. });
  203. view.on('error', (sender, tensor) => {
  204. this._raise('error', tensor);
  205. });
  206. const item = new sidebar.NameValueView(this._host, name, view);
  207. this._inputs.push(item);
  208. this._elements.push(item.render());
  209. }
  210. }
  211. _addOutput(name, output) {
  212. if (output.arguments.length > 0) {
  213. const item = new sidebar.NameValueView(this._host, name, new sidebar.ParameterView(this._host, output));
  214. this._outputs.push(item);
  215. this._elements.push(item.render());
  216. }
  217. }
  218. toggleInput(name) {
  219. for (const input of this._inputs) {
  220. if (name == input.name) {
  221. input.toggle();
  222. }
  223. }
  224. }
  225. on(event, callback) {
  226. this._events = this._events || {};
  227. this._events[event] = this._events[event] || [];
  228. this._events[event].push(callback);
  229. }
  230. _raise(event, data) {
  231. if (this._events && this._events[event]) {
  232. for (const callback of this._events[event]) {
  233. callback(this, data);
  234. }
  235. }
  236. }
  237. static formatAttributeValue(value, type, quote) {
  238. if (typeof value === 'function') {
  239. return value();
  240. }
  241. if (value && (value instanceof base.Int64 || value instanceof base.Uint64)) {
  242. return value.toString();
  243. }
  244. if (Number.isNaN(value)) {
  245. return 'NaN';
  246. }
  247. switch (type) {
  248. case 'shape':
  249. return value ? value.toString() : '(null)';
  250. case 'shape[]':
  251. if (value && !Array.isArray(value)) {
  252. throw new Error("Invalid shape '" + JSON.stringify(value) + "'.");
  253. }
  254. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  255. case 'graph':
  256. return value ? value.toString() : '(null)';
  257. case 'graph[]':
  258. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  259. case 'tensor':
  260. if (value && value.type && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length == 0) {
  261. return value.toString();
  262. }
  263. return '[...]';
  264. case 'function':
  265. return value.name;
  266. case 'function[]':
  267. return value ? value.map((item) => item.name).join(', ') : '(null)';
  268. }
  269. if (typeof value === 'string' && (!type || type != 'string')) {
  270. return quote ? '"' + value + '"' : value;
  271. }
  272. if (Array.isArray(value)) {
  273. if (value.length == 0) {
  274. return quote ? '[]' : '';
  275. }
  276. let ellipsis = false;
  277. if (value.length > 1000) {
  278. value = value.slice(0, 1000);
  279. ellipsis = true;
  280. }
  281. const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null;
  282. const array = value.map((item) => {
  283. if (item && (item instanceof base.Int64 || item instanceof base.Uint64)) {
  284. return item.toString();
  285. }
  286. if (Number.isNaN(item)) {
  287. return 'NaN';
  288. }
  289. const quote = !itemType || itemType === 'string';
  290. return sidebar.NodeSidebar.formatAttributeValue(item, itemType, quote);
  291. });
  292. if (ellipsis) {
  293. array.push('\u2026');
  294. }
  295. return quote ? [ '[', array.join(', '), ']' ].join(' ') : array.join(', ');
  296. }
  297. if (value === null) {
  298. return quote ? 'null' : '';
  299. }
  300. if (value === undefined) {
  301. return 'undefined';
  302. }
  303. if (value !== Object(value)) {
  304. return value.toString();
  305. }
  306. const list = [];
  307. const keys = Object.keys(value).filter((key) => !key.startsWith('__') && !key.endsWith('__'));
  308. if (keys.length == 1) {
  309. list.push(sidebar.NodeSidebar.formatAttributeValue(value[Object.keys(value)[0]], null, true));
  310. }
  311. else {
  312. for (const key of keys) {
  313. list.push(key + ': ' + sidebar.NodeSidebar.formatAttributeValue(value[key], null, true));
  314. }
  315. }
  316. let objectType = value.__type__;
  317. if (!objectType && value.constructor.name && value.constructor.name !== 'Object') {
  318. objectType = value.constructor.name;
  319. }
  320. if (objectType) {
  321. return objectType + (list.length == 0 ? '()' : [ '(', list.join(', '), ')' ].join(''));
  322. }
  323. switch (list.length) {
  324. case 0:
  325. return quote ? '()' : '';
  326. case 1:
  327. return list[0];
  328. default:
  329. return quote ? [ '(', list.join(', '), ')' ].join(' ') : list.join(', ');
  330. }
  331. }
  332. };
  333. sidebar.NameValueView = class {
  334. constructor(host, name, value) {
  335. this._host = host;
  336. this._name = name;
  337. this._value = value;
  338. const nameElement = this._host.document.createElement('div');
  339. nameElement.className = 'sidebar-view-item-name';
  340. const nameInputElement = this._host.document.createElement('input');
  341. nameInputElement.setAttribute('type', 'text');
  342. nameInputElement.setAttribute('value', name);
  343. nameInputElement.setAttribute('title', name);
  344. nameInputElement.setAttribute('readonly', 'true');
  345. nameElement.appendChild(nameInputElement);
  346. const valueElement = this._host.document.createElement('div');
  347. valueElement.className = 'sidebar-view-item-value-list';
  348. for (const element of value.render()) {
  349. valueElement.appendChild(element);
  350. }
  351. this._element = this._host.document.createElement('div');
  352. this._element.className = 'sidebar-view-item';
  353. this._element.appendChild(nameElement);
  354. this._element.appendChild(valueElement);
  355. }
  356. get name() {
  357. return this._name;
  358. }
  359. render() {
  360. return this._element;
  361. }
  362. toggle() {
  363. this._value.toggle();
  364. }
  365. };
  366. sidebar.SelectView = class {
  367. constructor(host, values, selected) {
  368. this._host = host;
  369. this._elements = [];
  370. const selectElement = this._host.document.createElement('select');
  371. selectElement.setAttribute('class', 'sidebar-view-item-select');
  372. selectElement.addEventListener('change', (e) => {
  373. this._raise('change', e.target.value);
  374. });
  375. this._elements.push(selectElement);
  376. for (const value of values) {
  377. const optionElement = this._host.document.createElement('option');
  378. optionElement.innerText = value;
  379. if (value == selected) {
  380. optionElement.setAttribute('selected', 'selected');
  381. }
  382. selectElement.appendChild(optionElement);
  383. }
  384. }
  385. render() {
  386. return this._elements;
  387. }
  388. on(event, callback) {
  389. this._events = this._events || {};
  390. this._events[event] = this._events[event] || [];
  391. this._events[event].push(callback);
  392. }
  393. _raise(event, data) {
  394. if (this._events && this._events[event]) {
  395. for (const callback of this._events[event]) {
  396. callback(this, data);
  397. }
  398. }
  399. }
  400. };
  401. sidebar.ValueTextView = class {
  402. constructor(host, value, action) {
  403. this._host = host;
  404. this._elements = [];
  405. const element = this._host.document.createElement('div');
  406. element.className = 'sidebar-view-item-value';
  407. this._elements.push(element);
  408. if (action) {
  409. this._action = this._host.document.createElement('div');
  410. this._action.className = 'sidebar-view-item-value-expander';
  411. this._action.innerHTML = action.text;
  412. this._action.addEventListener('click', () => {
  413. action.callback();
  414. });
  415. element.appendChild(this._action);
  416. }
  417. const list = Array.isArray(value) ? value : [ value ];
  418. let className = 'sidebar-view-item-value-line';
  419. for (const item of list) {
  420. const line = this._host.document.createElement('div');
  421. line.className = className;
  422. line.innerText = item;
  423. element.appendChild(line);
  424. className = 'sidebar-view-item-value-line-border';
  425. }
  426. }
  427. render() {
  428. return this._elements;
  429. }
  430. toggle() {
  431. }
  432. };
  433. class NodeAttributeView {
  434. constructor(host, attribute) {
  435. this._host = host;
  436. this._attribute = attribute;
  437. this._element = this._host.document.createElement('div');
  438. this._element.className = 'sidebar-view-item-value';
  439. const type = this._attribute.type;
  440. if (type) {
  441. this._expander = this._host.document.createElement('div');
  442. this._expander.className = 'sidebar-view-item-value-expander';
  443. this._expander.innerText = '+';
  444. this._expander.addEventListener('click', () => {
  445. this.toggle();
  446. });
  447. this._element.appendChild(this._expander);
  448. }
  449. const value = this._attribute.value;
  450. switch (type) {
  451. case 'function': {
  452. const line = this._host.document.createElement('div');
  453. line.className = 'sidebar-view-item-value-line-link';
  454. line.innerHTML = value.name;
  455. line.addEventListener('click', () => {
  456. this._raise('show-graph', value);
  457. });
  458. this._element.appendChild(line);
  459. break;
  460. }
  461. default: {
  462. let text = sidebar.NodeSidebar.formatAttributeValue(value, type);
  463. if (text && text.length > 1000) {
  464. text = text.substring(0, 1000) + '\u2026';
  465. }
  466. if (text && typeof text === 'string') {
  467. text = text.split('<').join('&lt;').split('>').join('&gt;');
  468. }
  469. const line = this._host.document.createElement('div');
  470. line.className = 'sidebar-view-item-value-line';
  471. line.innerHTML = (text ? text : '&nbsp;');
  472. this._element.appendChild(line);
  473. }
  474. }
  475. }
  476. render() {
  477. return [ this._element ];
  478. }
  479. toggle() {
  480. if (this._expander.innerText == '+') {
  481. this._expander.innerText = '-';
  482. const typeLine = this._host.document.createElement('div');
  483. typeLine.className = 'sidebar-view-item-value-line-border';
  484. const type = this._attribute.type;
  485. const value = this._attribute.value;
  486. if (type == 'tensor' && value && value.type) {
  487. typeLine.innerHTML = 'type: ' + '<code><b>' + value.type.toString() + '</b></code>';
  488. this._element.appendChild(typeLine);
  489. }
  490. else {
  491. typeLine.innerHTML = 'type: ' + '<code><b>' + this._attribute.type + '</b></code>';
  492. this._element.appendChild(typeLine);
  493. }
  494. const description = this._attribute.description;
  495. if (description) {
  496. const descriptionLine = this._host.document.createElement('div');
  497. descriptionLine.className = 'sidebar-view-item-value-line-border';
  498. descriptionLine.innerHTML = description;
  499. this._element.appendChild(descriptionLine);
  500. }
  501. if (this._attribute.type == 'tensor' && value) {
  502. const state = value.state;
  503. const valueLine = this._host.document.createElement('div');
  504. valueLine.className = 'sidebar-view-item-value-line-border';
  505. const contentLine = this._host.document.createElement('pre');
  506. contentLine.innerHTML = state || value.toString();
  507. valueLine.appendChild(contentLine);
  508. this._element.appendChild(valueLine);
  509. }
  510. }
  511. else {
  512. this._expander.innerText = '+';
  513. while (this._element.childElementCount > 2) {
  514. this._element.removeChild(this._element.lastChild);
  515. }
  516. }
  517. }
  518. on(event, callback) {
  519. this._events = this._events || {};
  520. this._events[event] = this._events[event] || [];
  521. this._events[event].push(callback);
  522. }
  523. _raise(event, data) {
  524. if (this._events && this._events[event]) {
  525. for (const callback of this._events[event]) {
  526. callback(this, data);
  527. }
  528. }
  529. }
  530. }
  531. sidebar.ParameterView = class {
  532. constructor(host, list) {
  533. this._list = list;
  534. this._elements = [];
  535. this._items = [];
  536. for (const argument of list.arguments) {
  537. const item = new sidebar.ArgumentView(host, argument);
  538. item.on('export-tensor', (sender, tensor) => {
  539. this._raise('export-tensor', tensor);
  540. });
  541. item.on('error', (sender, tensor) => {
  542. this._raise('error', tensor);
  543. });
  544. this._items.push(item);
  545. this._elements.push(item.render());
  546. }
  547. }
  548. render() {
  549. return this._elements;
  550. }
  551. toggle() {
  552. for (const item of this._items) {
  553. item.toggle();
  554. }
  555. }
  556. on(event, callback) {
  557. this._events = this._events || {};
  558. this._events[event] = this._events[event] || [];
  559. this._events[event].push(callback);
  560. }
  561. _raise(event, data) {
  562. if (this._events && this._events[event]) {
  563. for (const callback of this._events[event]) {
  564. callback(this, data);
  565. }
  566. }
  567. }
  568. };
  569. sidebar.ArgumentView = class {
  570. constructor(host, argument) {
  571. this._host = host;
  572. this._argument = argument;
  573. this._element = this._host.document.createElement('div');
  574. this._element.className = 'sidebar-view-item-value';
  575. const initializer = argument.initializer;
  576. if (initializer) {
  577. this._element.classList.add('sidebar-view-item-value-dark');
  578. }
  579. const quantization = argument.quantization;
  580. const type = argument.type;
  581. const location = this._argument.location !== undefined;
  582. if (type || initializer || quantization || location) {
  583. this._expander = this._host.document.createElement('div');
  584. this._expander.className = 'sidebar-view-item-value-expander';
  585. this._expander.innerText = '+';
  586. this._expander.addEventListener('click', () => {
  587. this.toggle();
  588. });
  589. this._element.appendChild(this._expander);
  590. }
  591. let name = this._argument.name || '';
  592. this._hasId = name ? true : false;
  593. this._hasKind = initializer && initializer.kind ? true : false;
  594. if (this._hasId || (!this._hasKind && !type)) {
  595. this._hasId = true;
  596. const nameLine = this._host.document.createElement('div');
  597. nameLine.className = 'sidebar-view-item-value-line';
  598. if (typeof name !== 'string') {
  599. throw new Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  600. }
  601. name = name.split('\n').shift(); // custom argument id
  602. name = name || ' ';
  603. nameLine.innerHTML = '<span class=\'sidebar-view-item-value-line-content\'>name: <b>' + name + '</b></span>';
  604. this._element.appendChild(nameLine);
  605. }
  606. else if (this._hasKind) {
  607. const kindLine = this._host.document.createElement('div');
  608. kindLine.className = 'sidebar-view-item-value-line';
  609. kindLine.innerHTML = 'kind: <b>' + initializer.kind + '</b>';
  610. this._element.appendChild(kindLine);
  611. }
  612. else if (type) {
  613. const typeLine = this._host.document.createElement('div');
  614. typeLine.className = 'sidebar-view-item-value-line-border';
  615. typeLine.innerHTML = 'type: <code><b>' + type.toString().split('<').join('&lt;').split('>').join('&gt;') + '</b></code>';
  616. this._element.appendChild(typeLine);
  617. }
  618. }
  619. render() {
  620. return this._element;
  621. }
  622. toggle() {
  623. if (this._expander) {
  624. if (this._expander.innerText == '+') {
  625. this._expander.innerText = '-';
  626. const initializer = this._argument.initializer;
  627. if (this._hasId && this._hasKind) {
  628. const kindLine = this._host.document.createElement('div');
  629. kindLine.className = 'sidebar-view-item-value-line-border';
  630. kindLine.innerHTML = 'kind: ' + '<b>' + initializer.kind + '</b>';
  631. this._element.appendChild(kindLine);
  632. }
  633. let type = null;
  634. let denotation = null;
  635. if (this._argument.type) {
  636. type = this._argument.type.toString();
  637. denotation = this._argument.type.denotation || null;
  638. }
  639. if (type && (this._hasId || this._hasKind)) {
  640. const typeLine = this._host.document.createElement('div');
  641. typeLine.className = 'sidebar-view-item-value-line-border';
  642. typeLine.innerHTML = 'type: <code><b>' + type.split('<').join('&lt;').split('>').join('&gt;') + '</b></code>';
  643. this._element.appendChild(typeLine);
  644. }
  645. if (denotation) {
  646. const denotationLine = this._host.document.createElement('div');
  647. denotationLine.className = 'sidebar-view-item-value-line-border';
  648. denotationLine.innerHTML = 'denotation: <code><b>' + denotation + '</b></code>';
  649. this._element.appendChild(denotationLine);
  650. }
  651. const description = this._argument.description;
  652. if (description) {
  653. const descriptionLine = this._host.document.createElement('div');
  654. descriptionLine.className = 'sidebar-view-item-value-line-border';
  655. descriptionLine.innerHTML = description;
  656. this._element.appendChild(descriptionLine);
  657. }
  658. const quantization = this._argument.quantization;
  659. if (quantization) {
  660. const quantizationLine = this._host.document.createElement('div');
  661. quantizationLine.className = 'sidebar-view-item-value-line-border';
  662. quantizationLine.innerHTML = '<span class=\'sidebar-view-item-value-line-content\'>quantization: ' + '<b>' + quantization + '</b></span>';
  663. this._element.appendChild(quantizationLine);
  664. }
  665. if (this._argument.location !== undefined) {
  666. const location = this._host.document.createElement('div');
  667. location.className = 'sidebar-view-item-value-line-border';
  668. location.innerHTML = 'location: ' + '<b>' + this._argument.location + '</b>';
  669. this._element.appendChild(location);
  670. }
  671. if (initializer) {
  672. const contentLine = this._host.document.createElement('pre');
  673. const valueLine = this._host.document.createElement('div');
  674. try {
  675. const state = initializer.state;
  676. if (state === null && this._host.save &&
  677. initializer.type.dataType && initializer.type.dataType != '?' &&
  678. initializer.type.shape && initializer.type.shape.dimensions && initializer.type.shape.dimensions.length > 0) {
  679. this._saveButton = this._host.document.createElement('div');
  680. this._saveButton.className = 'sidebar-view-item-value-expander';
  681. this._saveButton.innerHTML = '&#x1F4BE;';
  682. this._saveButton.addEventListener('click', () => {
  683. this._raise('export-tensor', initializer);
  684. });
  685. this._element.appendChild(this._saveButton);
  686. }
  687. valueLine.className = 'sidebar-view-item-value-line-border';
  688. contentLine.innerHTML = state || initializer.toString();
  689. }
  690. catch (err) {
  691. contentLine.innerHTML = err.toString();
  692. this._raise('error', err);
  693. }
  694. valueLine.appendChild(contentLine);
  695. this._element.appendChild(valueLine);
  696. }
  697. }
  698. else {
  699. this._expander.innerText = '+';
  700. while (this._element.childElementCount > 2) {
  701. this._element.removeChild(this._element.lastChild);
  702. }
  703. }
  704. }
  705. }
  706. on(event, callback) {
  707. this._events = this._events || {};
  708. this._events[event] = this._events[event] || [];
  709. this._events[event].push(callback);
  710. }
  711. _raise(event, data) {
  712. if (this._events && this._events[event]) {
  713. for (const callback of this._events[event]) {
  714. callback(this, data);
  715. }
  716. }
  717. }
  718. };
  719. sidebar.ModelSidebar = class {
  720. constructor(host, model, graph) {
  721. this._host = host;
  722. this._model = model;
  723. this._elements = [];
  724. if (model.format) {
  725. this._addProperty('format', new sidebar.ValueTextView(this._host, model.format));
  726. }
  727. if (model.producer) {
  728. this._addProperty('producer', new sidebar.ValueTextView(this._host, model.producer));
  729. }
  730. if (model.source) {
  731. this._addProperty('source', new sidebar.ValueTextView(this._host, model.source));
  732. }
  733. if (model.name) {
  734. this._addProperty('name', new sidebar.ValueTextView(this._host, model.name));
  735. }
  736. if (model.version) {
  737. this._addProperty('version', new sidebar.ValueTextView(this._host, model.version));
  738. }
  739. if (model.description) {
  740. this._addProperty('description', new sidebar.ValueTextView(this._host, model.description));
  741. }
  742. if (model.author) {
  743. this._addProperty('author', new sidebar.ValueTextView(this._host, model.author));
  744. }
  745. if (model.company) {
  746. this._addProperty('company', new sidebar.ValueTextView(this._host, model.company));
  747. }
  748. if (model.license) {
  749. this._addProperty('license', new sidebar.ValueTextView(this._host, model.license));
  750. }
  751. if (model.domain) {
  752. this._addProperty('domain', new sidebar.ValueTextView(this._host, model.domain));
  753. }
  754. if (model.imports) {
  755. this._addProperty('imports', new sidebar.ValueTextView(this._host, model.imports));
  756. }
  757. if (model.runtime) {
  758. this._addProperty('runtime', new sidebar.ValueTextView(this._host, model.runtime));
  759. }
  760. const metadata = model.metadata;
  761. if (metadata) {
  762. for (const property of model.metadata) {
  763. this._addProperty(property.name, new sidebar.ValueTextView(this._host, property.value));
  764. }
  765. }
  766. const graphs = Array.isArray(model.graphs) ? model.graphs : [];
  767. if (graphs.length > 1) {
  768. const name = graph && graph.name ? graph.name : '';
  769. const graphSelector = new sidebar.SelectView(this._host, model.graphs.map((g) => g.name), name);
  770. graphSelector.on('change', (sender, data) => {
  771. this._raise('update-active-graph', data);
  772. });
  773. this._addProperty('subgraph', graphSelector);
  774. }
  775. if (graph) {
  776. if (graph.version) {
  777. this._addProperty('version', new sidebar.ValueTextView(this._host, graph.version));
  778. }
  779. if (graph.type) {
  780. this._addProperty('type', new sidebar.ValueTextView(this._host, graph.type));
  781. }
  782. if (graph.tags) {
  783. this._addProperty('tags', new sidebar.ValueTextView(this._host, graph.tags));
  784. }
  785. if (graph.description) {
  786. this._addProperty('description', new sidebar.ValueTextView(this._host, graph.description));
  787. }
  788. if (graph.inputs.length > 0) {
  789. this._addHeader('Inputs');
  790. for (const input of graph.inputs) {
  791. this.addArgument(input.name, input);
  792. }
  793. }
  794. if (graph.outputs.length > 0) {
  795. this._addHeader('Outputs');
  796. for (const output of graph.outputs) {
  797. this.addArgument(output.name, output);
  798. }
  799. }
  800. }
  801. const separator = this._host.document.createElement('div');
  802. separator.className = 'sidebar-view-separator';
  803. this._elements.push(separator);
  804. }
  805. render() {
  806. return this._elements;
  807. }
  808. _addHeader(title) {
  809. const headerElement = this._host.document.createElement('div');
  810. headerElement.className = 'sidebar-view-header';
  811. headerElement.innerText = title;
  812. this._elements.push(headerElement);
  813. }
  814. _addProperty(name, value) {
  815. const item = new sidebar.NameValueView(this._host, name, value);
  816. this._elements.push(item.render());
  817. }
  818. addArgument(name, argument) {
  819. const view = new sidebar.ParameterView(this._host, argument);
  820. view.toggle();
  821. const item = new sidebar.NameValueView(this._host, name, view);
  822. this._elements.push(item.render());
  823. }
  824. on(event, callback) {
  825. this._events = this._events || {};
  826. this._events[event] = this._events[event] || [];
  827. this._events[event].push(callback);
  828. }
  829. _raise(event, data) {
  830. if (this._events && this._events[event]) {
  831. for (const callback of this._events[event]) {
  832. callback(this, data);
  833. }
  834. }
  835. }
  836. };
  837. sidebar.DocumentationSidebar = class {
  838. constructor(host, metadata) {
  839. this._host = host;
  840. this._metadata = metadata;
  841. }
  842. render() {
  843. if (!this._elements) {
  844. this._elements = [];
  845. const documentation = sidebar.DocumentationSidebar.formatDocumentation(this._metadata);
  846. const element = this._host.document.createElement('div');
  847. element.setAttribute('class', 'sidebar-view-documentation');
  848. this._append(element, 'h1', documentation.name);
  849. if (documentation.summary) {
  850. this._append(element, 'p', documentation.summary);
  851. }
  852. if (documentation.description) {
  853. this._append(element, 'p', documentation.description);
  854. }
  855. if (documentation.attributes) {
  856. this._append(element, 'h2', 'Attributes');
  857. const attributes = this._append(element, 'dl');
  858. for (const attribute of documentation.attributes) {
  859. this._append(attributes, 'dt', attribute.name + (attribute.type ? ': <tt>' + attribute.type + '</tt>' : ''));
  860. this._append(attributes, 'dd', attribute.description);
  861. }
  862. element.appendChild(attributes);
  863. }
  864. if (documentation.inputs) {
  865. this._append(element, 'h2', 'Inputs' + (documentation.inputs_range ? ' (' + documentation.inputs_range + ')' : ''));
  866. const inputs = this._append(element, 'dl');
  867. for (const input of documentation.inputs) {
  868. this._append(inputs, 'dt', input.name + (input.type ? ': <tt>' + input.type + '</tt>' : '') + (input.option ? ' (' + input.option + ')' : ''));
  869. this._append(inputs, 'dd', input.description);
  870. }
  871. }
  872. if (documentation.outputs) {
  873. this._append(element, 'h2', 'Outputs' + (documentation.outputs_range ? ' (' + documentation.outputs_range + ')' : ''));
  874. const outputs = this._append(element, 'dl');
  875. for (const output of documentation.outputs) {
  876. this._append(outputs, 'dt', output.name + (output.type ? ': <tt>' + output.type + '</tt>' : '') + (output.option ? ' (' + output.option + ')' : ''));
  877. this._append(outputs, 'dd', output.description);
  878. }
  879. }
  880. if (documentation.type_constraints) {
  881. this._append(element, 'h2', 'Type Constraints');
  882. const type_constraints = this._append(element, 'dl');
  883. for (const type_constraint of documentation.type_constraints) {
  884. this._append(type_constraints, 'dt', type_constraint.type_param_str + ': ' + type_constraint.allowed_type_strs.map((item) => '<tt>' + item + '</tt>').join(', '));
  885. this._append(type_constraints, 'dd', type_constraint.description);
  886. }
  887. }
  888. if (documentation.examples) {
  889. this._append(element, 'h2', 'Examples');
  890. for (const example of documentation.examples) {
  891. this._append(element, 'h3', example.summary);
  892. this._append(element, 'pre', example.code);
  893. }
  894. }
  895. if (documentation.references) {
  896. this._append(element, 'h2', 'References');
  897. const references = this._append(element, 'ul');
  898. for (const reference of documentation.references) {
  899. this._append(references, 'li', reference.description);
  900. }
  901. }
  902. if (documentation.domain && documentation.version && documentation.support_level) {
  903. this._append(element, 'h2', 'Support');
  904. this._append(element, 'dl', 'In domain <tt>' + documentation.domain + '</tt> since version <tt>' + documentation.version + '</tt> at support level <tt>' + documentation.support_level + '</tt>.');
  905. }
  906. if (!this._host.browser) {
  907. element.addEventListener('click', (e) => {
  908. if (e.target && e.target.href) {
  909. const link = e.target.href;
  910. if (link.startsWith('http://') || link.startsWith('https://')) {
  911. e.preventDefault();
  912. this._raise('navigate', { link: link });
  913. }
  914. }
  915. });
  916. }
  917. this._elements = [ element ];
  918. const separator = this._host.document.createElement('div');
  919. separator.className = 'sidebar-view-separator';
  920. this._elements.push(separator);
  921. }
  922. return this._elements;
  923. }
  924. on(event, callback) {
  925. this._events = this._events || {};
  926. this._events[event] = this._events[event] || [];
  927. this._events[event].push(callback);
  928. }
  929. _raise(event, data) {
  930. if (this._events && this._events[event]) {
  931. for (const callback of this._events[event]) {
  932. callback(this, data);
  933. }
  934. }
  935. }
  936. _append(parent, type, content) {
  937. const element = this._host.document.createElement(type);
  938. if (content) {
  939. element.innerHTML = content;
  940. }
  941. parent.appendChild(element);
  942. return element;
  943. }
  944. static formatDocumentation(source) {
  945. if (source) {
  946. const generator = new markdown.Generator();
  947. const target = {};
  948. if (source.name !== undefined) {
  949. target.name = source.name;
  950. }
  951. if (source.module !== undefined) {
  952. target.module = source.module;
  953. }
  954. if (source.category !== undefined) {
  955. target.category = source.category;
  956. }
  957. if (source.summary !== undefined) {
  958. target.summary = generator.html(source.summary);
  959. }
  960. if (source.description !== undefined) {
  961. target.description = generator.html(source.description);
  962. }
  963. if (Array.isArray(source.attributes)) {
  964. target.attributes = source.attributes.map((source) => {
  965. const target = {};
  966. target.name = source.name;
  967. if (source.type !== undefined) {
  968. target.type = source.type;
  969. }
  970. if (source.option !== undefined) {
  971. target.option = source.option;
  972. }
  973. if (source.optional !== undefined) {
  974. target.optional = source.optional;
  975. }
  976. if (source.required !== undefined) {
  977. target.required = source.required;
  978. }
  979. if (source.minimum !== undefined) {
  980. target.minimum = source.minimum;
  981. }
  982. if (source.src !== undefined) {
  983. target.src = source.src;
  984. }
  985. if (source.src_type !== undefined) {
  986. target.src_type = source.src_type;
  987. }
  988. if (source.description !== undefined) {
  989. target.description = generator.html(source.description);
  990. }
  991. if (source.default !== undefined) {
  992. target.default = source.default;
  993. }
  994. if (source.visible !== undefined) {
  995. target.visible = source.visible;
  996. }
  997. return target;
  998. });
  999. }
  1000. if (Array.isArray(source.inputs)) {
  1001. target.inputs = source.inputs.map((source) => {
  1002. const target = {};
  1003. target.name = source.name;
  1004. if (source.type !== undefined) {
  1005. target.type = source.type;
  1006. }
  1007. if (source.description !== undefined) {
  1008. target.description = generator.html(source.description);
  1009. }
  1010. if (source.default !== undefined) {
  1011. target.default = source.default;
  1012. }
  1013. if (source.src !== undefined) {
  1014. target.src = source.src;
  1015. }
  1016. if (source.list !== undefined) {
  1017. target.list = source.list;
  1018. }
  1019. if (source.isRef !== undefined) {
  1020. target.isRef = source.isRef;
  1021. }
  1022. if (source.typeAttr !== undefined) {
  1023. target.typeAttr = source.typeAttr;
  1024. }
  1025. if (source.numberAttr !== undefined) {
  1026. target.numberAttr = source.numberAttr;
  1027. }
  1028. if (source.typeListAttr !== undefined) {
  1029. target.typeListAttr = source.typeListAttr;
  1030. }
  1031. if (source.option !== undefined) {
  1032. target.option = source.option;
  1033. }
  1034. if (source.optional !== undefined) {
  1035. target.optional = source.optional;
  1036. }
  1037. if (source.visible !== undefined) {
  1038. target.visible = source.visible;
  1039. }
  1040. return target;
  1041. });
  1042. }
  1043. if (Array.isArray(source.outputs)) {
  1044. target.outputs = source.outputs.map((source) => {
  1045. const target = {};
  1046. target.name = source.name;
  1047. if (source.type) {
  1048. target.type = source.type;
  1049. }
  1050. if (source.description !== undefined) {
  1051. target.description = generator.html(source.description);
  1052. }
  1053. if (source.list !== undefined) {
  1054. target.list = source.list;
  1055. }
  1056. if (source.typeAttr !== undefined) {
  1057. target.typeAttr = source.typeAttr;
  1058. }
  1059. if (source.typeListAttr !== undefined) {
  1060. target.typeListAttr = source.typeAttr;
  1061. }
  1062. if (source.numberAttr !== undefined) {
  1063. target.numberAttr = source.numberAttr;
  1064. }
  1065. if (source.isRef !== undefined) {
  1066. target.isRef = source.isRef;
  1067. }
  1068. if (source.option !== undefined) {
  1069. target.option = source.option;
  1070. }
  1071. return target;
  1072. });
  1073. }
  1074. if (Array.isArray(source.references)) {
  1075. target.references = source.references.map((source) => {
  1076. if (source) {
  1077. target.description = generator.html(source.description);
  1078. }
  1079. return target;
  1080. });
  1081. }
  1082. if (source.version !== undefined) {
  1083. target.version = source.version;
  1084. }
  1085. if (source.operator !== undefined) {
  1086. target.operator = source.operator;
  1087. }
  1088. if (source.identifier !== undefined) {
  1089. target.identifier = source.identifier;
  1090. }
  1091. if (source.package !== undefined) {
  1092. target.package = source.package;
  1093. }
  1094. if (source.support_level !== undefined) {
  1095. target.support_level = source.support_level;
  1096. }
  1097. if (source.min_input !== undefined) {
  1098. target.min_input = source.min_input;
  1099. }
  1100. if (source.max_input !== undefined) {
  1101. target.max_input = source.max_input;
  1102. }
  1103. if (source.min_output !== undefined) {
  1104. target.min_output = source.min_output;
  1105. }
  1106. if (source.max_input !== undefined) {
  1107. target.max_output = source.max_output;
  1108. }
  1109. if (source.inputs_range !== undefined) {
  1110. target.inputs_range = source.inputs_range;
  1111. }
  1112. if (source.outputs_range !== undefined) {
  1113. target.outputs_range = source.outputs_range;
  1114. }
  1115. if (source.examples !== undefined) {
  1116. target.examples = source.examples;
  1117. }
  1118. if (source.constants !== undefined) {
  1119. target.constants = source.constants;
  1120. }
  1121. if (source.type_constraints !== undefined) {
  1122. target.type_constraints = source.type_constraints;
  1123. }
  1124. return target;
  1125. }
  1126. return '';
  1127. }
  1128. };
  1129. sidebar.FindSidebar = class {
  1130. constructor(host, element, graph) {
  1131. this._host = host;
  1132. this._graphElement = element;
  1133. this._graph = graph;
  1134. this._contentElement = this._host.document.createElement('div');
  1135. this._contentElement.setAttribute('class', 'sidebar-view-find');
  1136. this._searchElement = this._host.document.createElement('input');
  1137. this._searchElement.setAttribute('id', 'search');
  1138. this._searchElement.setAttribute('type', 'text');
  1139. this._searchElement.setAttribute('placeholder', 'Search...');
  1140. this._searchElement.setAttribute('style', 'width: 100%');
  1141. this._searchElement.addEventListener('input', (e) => {
  1142. this.update(e.target.value);
  1143. this._raise('search-text-changed', e.target.value);
  1144. });
  1145. this._resultElement = this._host.document.createElement('ol');
  1146. this._resultElement.addEventListener('click', (e) => {
  1147. this.select(e);
  1148. });
  1149. this._contentElement.appendChild(this._searchElement);
  1150. this._contentElement.appendChild(this._resultElement);
  1151. }
  1152. on(event, callback) {
  1153. this._events = this._events || {};
  1154. this._events[event] = this._events[event] || [];
  1155. this._events[event].push(callback);
  1156. }
  1157. _raise(event, data) {
  1158. if (this._events && this._events[event]) {
  1159. for (const callback of this._events[event]) {
  1160. callback(this, data);
  1161. }
  1162. }
  1163. }
  1164. select(e) {
  1165. const selection = [];
  1166. const id = e.target.id;
  1167. const nodesElement = this._graphElement.getElementById('nodes');
  1168. let nodeElement = nodesElement.firstChild;
  1169. while (nodeElement) {
  1170. if (nodeElement.id == id) {
  1171. selection.push(nodeElement);
  1172. }
  1173. nodeElement = nodeElement.nextSibling;
  1174. }
  1175. const edgePathsElement = this._graphElement.getElementById('edge-paths');
  1176. let edgePathElement = edgePathsElement.firstChild;
  1177. while (edgePathElement) {
  1178. if (edgePathElement.id == id) {
  1179. selection.push(edgePathElement);
  1180. }
  1181. edgePathElement = edgePathElement.nextSibling;
  1182. }
  1183. let initializerElement = this._graphElement.getElementById(id);
  1184. if (initializerElement) {
  1185. while (initializerElement.parentElement) {
  1186. initializerElement = initializerElement.parentElement;
  1187. if (initializerElement.id && initializerElement.id.startsWith('node-')) {
  1188. selection.push(initializerElement);
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. if (selection.length > 0) {
  1194. this._raise('select', selection);
  1195. }
  1196. }
  1197. focus(searchText) {
  1198. this._searchElement.focus();
  1199. this._searchElement.value = '';
  1200. this._searchElement.value = searchText;
  1201. this.update(searchText);
  1202. }
  1203. update(searchText) {
  1204. while (this._resultElement.lastChild) {
  1205. this._resultElement.removeChild(this._resultElement.lastChild);
  1206. }
  1207. const terms = searchText.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0);
  1208. const nodes = new Set();
  1209. const edges = new Set();
  1210. for (const node of this._graph.nodes) {
  1211. const initializers = [];
  1212. for (const input of node.inputs) {
  1213. for (const argument of input.arguments) {
  1214. if (argument.name && !edges.has(argument.name)) {
  1215. const match = (argument, term) => {
  1216. if (argument.name && argument.name.toLowerCase().indexOf(term) !== -1) {
  1217. return true;
  1218. }
  1219. if (argument.type) {
  1220. if (argument.type.dataType && term === argument.type.dataType.toLowerCase()) {
  1221. return true;
  1222. }
  1223. if (argument.type.shape) {
  1224. if (term === argument.type.shape.toString().toLowerCase()) {
  1225. return true;
  1226. }
  1227. if (argument.type.shape && Array.isArray(argument.type.shape.dimensions)) {
  1228. const dimensions = argument.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : '');
  1229. if (term === dimensions.join(',')) {
  1230. return true;
  1231. }
  1232. if (dimensions.some((dimension) => term === dimension)) {
  1233. return true;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. return false;
  1239. };
  1240. if (terms.every((term) => match(argument, term))) {
  1241. if (!argument.initializer) {
  1242. const inputItem = this._host.document.createElement('li');
  1243. inputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id
  1244. inputItem.id = 'edge-' + argument.name;
  1245. this._resultElement.appendChild(inputItem);
  1246. edges.add(argument.name);
  1247. }
  1248. else {
  1249. initializers.push(argument);
  1250. }
  1251. }
  1252. }
  1253. }
  1254. }
  1255. const name = node.name;
  1256. const type = node.type.name;
  1257. if (name && !nodes.has(name) &&
  1258. terms.every((term) => name.toLowerCase().indexOf(term) != -1 || (type && type.toLowerCase().indexOf(term) != -1))) {
  1259. const nameItem = this._host.document.createElement('li');
  1260. nameItem.innerText = '\u25A2 ' + node.name;
  1261. nameItem.id = 'node-name-' + node.name;
  1262. this._resultElement.appendChild(nameItem);
  1263. nodes.add(node.name);
  1264. }
  1265. for (const argument of initializers) {
  1266. if (argument.name) {
  1267. const initializeItem = this._host.document.createElement('li');
  1268. initializeItem.innerText = '\u25A0 ' + argument.name;
  1269. initializeItem.id = 'initializer-' + argument.name;
  1270. this._resultElement.appendChild(initializeItem);
  1271. }
  1272. }
  1273. }
  1274. for (const node of this._graph.nodes) {
  1275. for (const output of node.outputs) {
  1276. for (const argument of output.arguments) {
  1277. if (argument.name && !edges.has(argument.name) && terms.every((term) => argument.name.toLowerCase().indexOf(term) != -1)) {
  1278. const outputItem = this._host.document.createElement('li');
  1279. outputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id
  1280. outputItem.id = 'edge-' + argument.name;
  1281. this._resultElement.appendChild(outputItem);
  1282. edges.add(argument.name);
  1283. }
  1284. }
  1285. }
  1286. }
  1287. this._resultElement.style.display = this._resultElement.childNodes.length != 0 ? 'block' : 'none';
  1288. }
  1289. get content() {
  1290. return this._contentElement;
  1291. }
  1292. };
  1293. const markdown = {};
  1294. markdown.Generator = class {
  1295. constructor() {
  1296. this._newlineRegExp = /^\n+/;
  1297. this._codeRegExp = /^( {4}[^\n]+\n*)+/;
  1298. this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/;
  1299. this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/;
  1300. this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/;
  1301. this._blockquoteRegExp = /^( {0,3}> ?(([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)|[^\n]*)(?:\n|$))+/;
  1302. this._listRegExp = /^( {0,3})((?:[*+-]|\d{1,9}[.)])) [\s\S]+?(?:\n+(?=\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$))|\n+(?= {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d{1,9}[.)]) )\n*|\s*$)/;
  1303. this._htmlRegExp = /^ {0,3}(?:<(script|pre|style)[\s>][\s\S]*?(?:<\/\1>[^\n]*\n+|$)|<!--(?!-?>)[\s\S]*?(?:-->|$)[^\n]*(\n+|$)|<\?[\s\S]*?(?:\?>\n*|$)|<![A-Z][\s\S]*?(?:>\n*|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>\n*|$)|<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)[\s\S]*?(?:\n{2,}|$)|<(?!script|pre|style)([a-z][\w-]*)(?: +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?)*? *\/?>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$)|<\/(?!script|pre|style)[a-z][\w-]*\s*>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$))/i;
  1304. this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/;
  1305. this._nptableRegExp = /^ *([^|\n ].*\|.*)\n {0,3}([-:]+ *\|[-| :]*)(?:\n((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  1306. this._tableRegExp = /^ *\|(.+)\n {0,3}\|?( *[-:]+[-| :]*)(?:\n *((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  1307. this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/;
  1308. this._textRegExp = /^[^\n]+/;
  1309. this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/;
  1310. this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm;
  1311. this._paragraphRegExp = /^([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/;
  1312. this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/;
  1313. this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/;
  1314. this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g;
  1315. /* eslint-disable no-control-regex */
  1316. this._autolinkRegExp = /^<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s\x00-\x1f<>]*|[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_]))>/;
  1317. this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
  1318. /* eslint-enable no-control-regex */
  1319. this._urlRegExp = /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9-]+\.?)+[^\s<]*|^[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/i;
  1320. this._tagRegExp = /^<!--(?!-?>)[\s\S]*?-->|^<\/[a-zA-Z][\w:-]*\s*>|^<[a-zA-Z][\w-]*(?:\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?)*?\s*\/?>|^<\?[\s\S]*?\?>|^<![a-zA-Z]+\s[\s\S]*?>|^<!\[CDATA\[[\s\S]*?\]\]>/;
  1321. this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/;
  1322. this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/;
  1323. this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g;
  1324. this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/;
  1325. this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/;
  1326. this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  1327. this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  1328. this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/;
  1329. this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/;
  1330. this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  1331. this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g,
  1332. this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
  1333. this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/;
  1334. this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/;
  1335. this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<![`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+/=?_`{|}~-](?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))/;
  1336. this._punctuationRegExp = /^([\s*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~])/;
  1337. this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g;
  1338. this._escapeTestRegExp = /[&<>"']/;
  1339. this._escapeReplaceRegExp = /[&<>"']/g;
  1340. this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/;
  1341. this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g;
  1342. this._escapeReplacementsMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
  1343. }
  1344. html(source) {
  1345. const tokens = [];
  1346. const links = new Map();
  1347. this._tokenize(source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '), tokens, links, true);
  1348. this._tokenizeBlock(tokens, links);
  1349. const slugs = new Map();
  1350. const result = this._render(tokens, slugs, true);
  1351. return result;
  1352. }
  1353. _tokenize(source, tokens, links, top) {
  1354. source = source.replace(/^ +$/gm, '');
  1355. while (source) {
  1356. let match = this._newlineRegExp.exec(source);
  1357. if (match) {
  1358. source = source.substring(match[0].length);
  1359. if (match[0].length > 1) {
  1360. tokens.push({ type: 'space' });
  1361. }
  1362. continue;
  1363. }
  1364. match = this._codeRegExp.exec(source);
  1365. if (match) {
  1366. source = source.substring(match[0].length);
  1367. const lastToken = tokens[tokens.length - 1];
  1368. if (lastToken && lastToken.type === 'paragraph') {
  1369. lastToken.text += '\n' + match[0].trimRight();
  1370. }
  1371. else {
  1372. const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, '');
  1373. tokens.push({ type: 'code', text: text });
  1374. }
  1375. continue;
  1376. }
  1377. match = this._fencesRegExp.exec(source);
  1378. if (match) {
  1379. source = source.substring(match[0].length);
  1380. const language = match[2] ? match[2].trim() : match[2];
  1381. let text = match[3] || '';
  1382. const matchIndent = match[0].match(/^(\s+)(?:```)/);
  1383. if (matchIndent !== null) {
  1384. const indent = matchIndent[1];
  1385. text = text.split('\n').map(node => {
  1386. const match = node.match(/^\s+/);
  1387. return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
  1388. }).join('\n');
  1389. }
  1390. tokens.push({ type: 'code', language: language, text: text });
  1391. continue;
  1392. }
  1393. match = this._headingRegExp.exec(source);
  1394. if (match) {
  1395. source = source.substring(match[0].length);
  1396. tokens.push({ type: 'heading', depth: match[1].length, text: match[2] });
  1397. continue;
  1398. }
  1399. match = this._nptableRegExp.exec(source);
  1400. if (match) {
  1401. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  1402. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  1403. if (header.length === align.length) {
  1404. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  1405. const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
  1406. for (let i = 0; i < token.align.length; i++) {
  1407. if (/^ *-+: *$/.test(token.align[i])) {
  1408. token.align[i] = 'right';
  1409. }
  1410. else if (/^ *:-+: *$/.test(token.align[i])) {
  1411. token.align[i] = 'center';
  1412. }
  1413. else if (/^ *:-+ *$/.test(token.align[i])) {
  1414. token.align[i] = 'left';
  1415. }
  1416. else {
  1417. token.align[i] = null;
  1418. }
  1419. }
  1420. token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length));
  1421. source = source.substring(token.raw.length);
  1422. tokens.push(token);
  1423. continue;
  1424. }
  1425. }
  1426. match = this._hrRegExp.exec(source);
  1427. if (match) {
  1428. source = source.substring(match[0].length);
  1429. tokens.push({ type: 'hr' });
  1430. continue;
  1431. }
  1432. match = this._blockquoteRegExp.exec(source);
  1433. if (match) {
  1434. source = source.substring(match[0].length);
  1435. const text = match[0].replace(/^ *> ?/gm, '');
  1436. tokens.push({ type: 'blockquote', text: text, tokens: this._tokenize(text, [], links, top) });
  1437. continue;
  1438. }
  1439. match = this._listRegExp.exec(source);
  1440. if (match) {
  1441. let raw = match[0];
  1442. const bull = match[2];
  1443. const ordered = bull.length > 1;
  1444. const parent = bull[bull.length - 1] === ')';
  1445. const list = { type: 'list', raw: raw, ordered: ordered, start: ordered ? +bull.slice(0, -1) : '', loose: false, items: [] };
  1446. const itemMatch = match[0].match(this._itemRegExp);
  1447. let next = false;
  1448. const length = itemMatch.length;
  1449. for (let i = 0; i < length; i++) {
  1450. let item = itemMatch[i];
  1451. raw = item;
  1452. let space = item.length;
  1453. item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
  1454. if (~item.indexOf('\n ')) {
  1455. space -= item.length;
  1456. item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '');
  1457. }
  1458. if (i !== length - 1) {
  1459. const bullet = this._bulletRegExp.exec(itemMatch[i + 1])[0];
  1460. if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) {
  1461. const addBack = itemMatch.slice(i + 1).join('\n');
  1462. list.raw = list.raw.substring(0, list.raw.length - addBack.length);
  1463. i = length - 1;
  1464. }
  1465. }
  1466. let loose = next || /\n\n(?!\s*$)/.test(item);
  1467. if (i !== length - 1) {
  1468. next = item.charAt(item.length - 1) === '\n';
  1469. if (!loose) {
  1470. loose = next;
  1471. }
  1472. }
  1473. if (loose) {
  1474. list.loose = true;
  1475. }
  1476. const task = /^\[[ xX]\] /.test(item);
  1477. let checked = undefined;
  1478. if (task) {
  1479. checked = item[1] !== ' ';
  1480. item = item.replace(/^\[[ xX]\] +/, '');
  1481. }
  1482. list.items.push({ type: 'list_item', raw, task: task, checked: checked, loose: loose, text: item });
  1483. }
  1484. source = source.substring(list.raw.length);
  1485. for (const item of list.items) {
  1486. item.tokens = this._tokenize(item.text, [], links, false);
  1487. }
  1488. tokens.push(list);
  1489. continue;
  1490. }
  1491. match = this._htmlRegExp.exec(source);
  1492. if (match) {
  1493. source = source.substring(match[0].length);
  1494. tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] });
  1495. continue;
  1496. }
  1497. if (top) {
  1498. match = this._defRegExp.exec(source);
  1499. if (match) {
  1500. source = source.substring(match[0].length);
  1501. match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3];
  1502. const tag = match[1].toLowerCase().replace(/\s+/g, ' ');
  1503. if (!links.has(tag)) {
  1504. links.set(tag, { href: match[2], title: match[3] });
  1505. }
  1506. continue;
  1507. }
  1508. }
  1509. match = this._tableRegExp.exec(source);
  1510. if (match) {
  1511. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  1512. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  1513. if (header.length === align.length) {
  1514. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  1515. const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
  1516. for (let i = 0; i < token.align.length; i++) {
  1517. if (/^ *-+: *$/.test(token.align[i])) {
  1518. token.align[i] = 'right';
  1519. }
  1520. else if (/^ *:-+: *$/.test(token.align[i])) {
  1521. token.align[i] = 'center';
  1522. }
  1523. else if (/^ *:-+ *$/.test(token.align[i])) {
  1524. token.align[i] = 'left';
  1525. }
  1526. else {
  1527. token.align[i] = null;
  1528. }
  1529. }
  1530. token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length));
  1531. source = source.substring(token.raw.length);
  1532. tokens.push(token);
  1533. continue;
  1534. }
  1535. }
  1536. match = this._lheadingRegExp.exec(source);
  1537. if (match) {
  1538. source = source.substring(match[0].length);
  1539. tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] });
  1540. continue;
  1541. }
  1542. if (top) {
  1543. match = this._paragraphRegExp.exec(source);
  1544. if (match) {
  1545. source = source.substring(match[0].length);
  1546. tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] });
  1547. continue;
  1548. }
  1549. }
  1550. match = this._textRegExp.exec(source);
  1551. if (match) {
  1552. source = source.substring(match[0].length);
  1553. const lastToken = tokens[tokens.length - 1];
  1554. if (lastToken && lastToken.type === 'text') {
  1555. lastToken.text += '\n' + match[0];
  1556. }
  1557. else {
  1558. tokens.push({ type: 'text', text: match[0] });
  1559. }
  1560. continue;
  1561. }
  1562. throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
  1563. }
  1564. return tokens;
  1565. }
  1566. _tokenizeInline(source, links, inLink, inRawBlock, prevChar) {
  1567. const tokens = [];
  1568. let maskedSource = source;
  1569. if (links.size > 0) {
  1570. while (maskedSource) {
  1571. const match = this._reflinkSearchRegExp.exec(maskedSource);
  1572. if (match) {
  1573. if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  1574. maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._reflinkSearchRegExp.lastIndex);
  1575. }
  1576. continue;
  1577. }
  1578. break;
  1579. }
  1580. }
  1581. while (maskedSource) {
  1582. const match = this._blockSkipRegExp.exec(maskedSource);
  1583. if (match) {
  1584. maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._blockSkipRegExp.lastIndex);
  1585. continue;
  1586. }
  1587. break;
  1588. }
  1589. while (source) {
  1590. let match = this._escapeRegExp.exec(source);
  1591. if (match) {
  1592. source = source.substring(match[0].length);
  1593. tokens.push({ type: 'escape', text: this._escape(match[1]) });
  1594. continue;
  1595. }
  1596. match = this._tagRegExp.exec(source);
  1597. if (match) {
  1598. source = source.substring(match[0].length);
  1599. if (!inLink && /^<a /i.test(match[0])) {
  1600. inLink = true;
  1601. }
  1602. else if (inLink && /^<\/a>/i.test(match[0])) {
  1603. inLink = false;
  1604. }
  1605. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  1606. inRawBlock = true;
  1607. }
  1608. else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  1609. inRawBlock = false;
  1610. }
  1611. tokens.push({ type: 'html', raw: match[0], text: match[0] });
  1612. continue;
  1613. }
  1614. match = this._linkRegExp.exec(source);
  1615. if (match) {
  1616. let index = -1;
  1617. const ref = match[2];
  1618. if (ref.indexOf(')') !== -1) {
  1619. let level = 0;
  1620. for (let i = 0; i < ref.length; i++) {
  1621. switch (ref[i]) {
  1622. case '\\':
  1623. i++;
  1624. break;
  1625. case '(':
  1626. level++;
  1627. break;
  1628. case ')':
  1629. level--;
  1630. if (level < 0) {
  1631. index = i;
  1632. i = ref.length;
  1633. }
  1634. break;
  1635. }
  1636. }
  1637. }
  1638. if (index > -1) {
  1639. const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index;
  1640. match[2] = match[2].substring(0, index);
  1641. match[0] = match[0].substring(0, length).trim();
  1642. match[3] = '';
  1643. }
  1644. const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1');
  1645. const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1');
  1646. const token = this._outputLink(match, href, title);
  1647. source = source.substring(match[0].length);
  1648. if (token.type === 'link') {
  1649. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  1650. }
  1651. tokens.push(token);
  1652. continue;
  1653. }
  1654. match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source);
  1655. if (match) {
  1656. let link = (match[2] || match[1]).replace(/\s+/g, ' ');
  1657. link = links.get(link.toLowerCase());
  1658. if (!link || !link.href) {
  1659. const text = match[0].charAt(0);
  1660. source = source.substring(text.length);
  1661. tokens.push({ type: 'text', text: text });
  1662. }
  1663. else {
  1664. source = source.substring(match[0].length);
  1665. const token = this._outputLink(match, link);
  1666. if (token.type === 'link') {
  1667. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  1668. }
  1669. tokens.push(token);
  1670. }
  1671. continue;
  1672. }
  1673. match = this._strongStartRegExp.exec(source);
  1674. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  1675. const masked = maskedSource.slice(-1 * source.length);
  1676. const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp;
  1677. endReg.lastIndex = 0;
  1678. let cap;
  1679. while ((match = endReg.exec(masked)) != null) {
  1680. cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3));
  1681. if (cap) {
  1682. break;
  1683. }
  1684. }
  1685. if (cap) {
  1686. const text = source.substring(2, cap[0].length - 2);
  1687. source = source.substring(cap[0].length);
  1688. tokens.push({ type: 'strong', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1689. continue;
  1690. }
  1691. }
  1692. match = this._emStartRegExp.exec(source);
  1693. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  1694. const masked = maskedSource.slice(-1 * source.length);
  1695. const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp;
  1696. endReg.lastIndex = 0;
  1697. let cap;
  1698. while ((match = endReg.exec(masked)) != null) {
  1699. cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2));
  1700. if (cap) {
  1701. break;
  1702. }
  1703. }
  1704. if (cap) {
  1705. const text = source.slice(1, cap[0].length - 1);
  1706. source = source.substring(cap[0].length);
  1707. tokens.push({ type: 'em', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1708. continue;
  1709. }
  1710. }
  1711. match = this._codespanRegExp.exec(source);
  1712. if (match) {
  1713. source = source.substring(match[0].length);
  1714. let text = match[2].replace(/\n/g, ' ');
  1715. if (/[^ ]/.test(text) && text.startsWith(' ') && text.endsWith(' ')) {
  1716. text = text.substring(1, text.length - 1);
  1717. }
  1718. tokens.push({ type: 'codespan', text: this._encode(text) });
  1719. continue;
  1720. }
  1721. match = this._brRegExp.exec(source);
  1722. if (match) {
  1723. source = source.substring(match[0].length);
  1724. tokens.push({ type: 'br' });
  1725. continue;
  1726. }
  1727. match = this._delRegExp.exec(source);
  1728. if (match) {
  1729. source = source.substring(match[0].length);
  1730. const text = match[1];
  1731. tokens.push({ type: 'del', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1732. continue;
  1733. }
  1734. match = this._autolinkRegExp.exec(source);
  1735. if (match) {
  1736. source = source.substring(match[0].length);
  1737. const text = this._escape(match[1]);
  1738. const href = match[2] === '@' ? 'mailto:' + text : text;
  1739. tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', raw: text, text } ] });
  1740. continue;
  1741. }
  1742. if (!inLink) {
  1743. match = this._urlRegExp.exec(source);
  1744. if (match) {
  1745. const email = match[2] === '@';
  1746. if (!email) {
  1747. let prevCapZero;
  1748. do {
  1749. prevCapZero = match[0];
  1750. match[0] = this._backpedalRegExp.exec(match[0])[0];
  1751. } while (prevCapZero !== match[0]);
  1752. }
  1753. const text = this._escape(match[0]);
  1754. const href = email ? ('mailto:' + text) : (match[1] === 'www.' ? 'http://' + text : text);
  1755. source = source.substring(match[0].length);
  1756. tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', text: text } ] });
  1757. continue;
  1758. }
  1759. }
  1760. match = this._textspanRegExp.exec(source);
  1761. if (match) {
  1762. source = source.substring(match[0].length);
  1763. prevChar = match[0].slice(-1);
  1764. tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) });
  1765. continue;
  1766. }
  1767. throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
  1768. }
  1769. return tokens;
  1770. }
  1771. _tokenizeBlock(tokens, links) {
  1772. for (const token of tokens) {
  1773. switch (token.type) {
  1774. case 'paragraph':
  1775. case 'text':
  1776. case 'heading': {
  1777. token.tokens = this._tokenizeInline(token.text, links, false, false, '');
  1778. break;
  1779. }
  1780. case 'table': {
  1781. token.tokens = {};
  1782. token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, ''));
  1783. token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, '')));
  1784. break;
  1785. }
  1786. case 'blockquote': {
  1787. this._tokenizeBlock(token.tokens, links);
  1788. break;
  1789. }
  1790. case 'list': {
  1791. for (const item of token.items) {
  1792. this._tokenizeBlock(item.tokens, links);
  1793. }
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. }
  1799. _render(tokens, slugs, top) {
  1800. let html = '';
  1801. while (tokens.length > 0) {
  1802. const token = tokens.shift();
  1803. switch (token.type) {
  1804. case 'space': {
  1805. continue;
  1806. }
  1807. case 'hr': {
  1808. html += '<hr>\n';
  1809. continue;
  1810. }
  1811. case 'heading': {
  1812. const level = token.depth;
  1813. const id = this._slug(slugs, this._renderInline(token.tokens, true));
  1814. html += '<h' + level + ' id="' + id + '">' + this._renderInline(token.tokens) + '</h' + level + '>\n';
  1815. continue;
  1816. }
  1817. case 'code': {
  1818. const code = token.text;
  1819. const language = (token.language || '').match(/\S*/)[0];
  1820. html += '<pre><code' + (language ? ' class="' + 'language-' + this._encode(language) + '"' : '') + '>' + (token.escaped ? code : this._encode(code)) + '</code></pre>\n';
  1821. continue;
  1822. }
  1823. case 'table': {
  1824. let header = '';
  1825. let cell = '';
  1826. for (let j = 0; j < token.header.length; j++) {
  1827. const content = this._renderInline(token.tokens.header[j]);
  1828. const align = token.align[j];
  1829. cell += '<th' + (align ? ' align="' + align + '"' : '') + '>' + content + '</th>\n';
  1830. }
  1831. header += '<tr>\n' + cell + '</tr>\n';
  1832. let body = '';
  1833. for (let j = 0; j < token.cells.length; j++) {
  1834. const row = token.tokens.cells[j];
  1835. cell = '';
  1836. for (let k = 0; k < row.length; k++) {
  1837. const content = this._renderInline(row[k]);
  1838. const align = token.align[k];
  1839. cell += '<td' + (align ? ' align="' + align + '"' : '') + '>' + content + '</td>\n';
  1840. }
  1841. body += '<tr>\n' + cell + '</tr>\n';
  1842. }
  1843. html += '<table>\n<thead>\n' + header + '</thead>\n' + (body ? '<tbody>' + body + '</tbody>' : body) + '</table>\n';
  1844. continue;
  1845. }
  1846. case 'blockquote': {
  1847. html += '<blockquote>\n' + this._render(token.tokens, slugs, true) + '</blockquote>\n';
  1848. continue;
  1849. }
  1850. case 'list': {
  1851. const ordered = token.ordered;
  1852. const start = token.start;
  1853. const loose = token.loose;
  1854. let body = '';
  1855. for (const item of token.items) {
  1856. let itemBody = '';
  1857. if (item.task) {
  1858. const checkbox = '<input ' + (item.checked ? 'checked="" ' : '') + 'disabled="" type="checkbox"' + '> ';
  1859. if (loose) {
  1860. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  1861. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1862. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1863. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1864. }
  1865. }
  1866. else {
  1867. item.tokens.unshift({ type: 'text', text: checkbox });
  1868. }
  1869. }
  1870. else {
  1871. itemBody += checkbox;
  1872. }
  1873. }
  1874. itemBody += this._render(item.tokens, slugs, loose);
  1875. body += '<li>' + itemBody + '</li>\n';
  1876. }
  1877. const type = (ordered ? 'ol' : 'ul');
  1878. html += '<' + type + (ordered && start !== 1 ? (' start="' + start + '"') : '') + '>\n' + body + '</' + type + '>\n';
  1879. continue;
  1880. }
  1881. case 'html': {
  1882. html += token.text;
  1883. continue;
  1884. }
  1885. case 'paragraph': {
  1886. html += '<p>' + this._renderInline(token.tokens) + '</p>\n';
  1887. continue;
  1888. }
  1889. case 'text': {
  1890. html += top ? '<p>' : '';
  1891. html += token.tokens ? this._renderInline(token.tokens) : token.text;
  1892. while (tokens.length > 0 && tokens[0].type === 'text') {
  1893. const token = tokens.shift();
  1894. html += '\n' + (token.tokens ? this._renderInline(token.tokens) : token.text);
  1895. }
  1896. html += top ? '</p>\n' : '';
  1897. continue;
  1898. }
  1899. default: {
  1900. throw new Error("Unexpected token type '" + token.type + "'.");
  1901. }
  1902. }
  1903. }
  1904. return html;
  1905. }
  1906. _renderInline(tokens, slug) {
  1907. let html = '';
  1908. for (const token of tokens) {
  1909. switch (token.type) {
  1910. case 'escape':
  1911. case 'html':
  1912. case 'text': {
  1913. html += token.text;
  1914. break;
  1915. }
  1916. case 'link': {
  1917. const text = this._renderInline(token.tokens, slug);
  1918. html += slug ? text : '<a href="' + token.href + '"' + (token.title ? ' title="' + token.title + '"' : '') + ' target="_blank">' + text + '</a>';
  1919. break;
  1920. }
  1921. case 'image': {
  1922. html += slug ? token.text : '<img src="' + token.href + '" alt="' + token.text + '"' + (token.title ? ' title="' + token.title + '"' : '') + '>';
  1923. break;
  1924. }
  1925. case 'strong': {
  1926. const text = this._renderInline(token.tokens, slug);
  1927. html += slug ? text : '<strong>' + text + '</strong>';
  1928. break;
  1929. }
  1930. case 'em': {
  1931. const text = this._renderInline(token.tokens, slug);
  1932. html += slug ? text : '<em>' + text + '</em>';
  1933. break;
  1934. }
  1935. case 'codespan': {
  1936. html += slug ? token.text : '<code>' + token.text + '</code>';
  1937. break;
  1938. }
  1939. case 'br': {
  1940. html += slug ? '' : '<br>';
  1941. break;
  1942. }
  1943. case 'del': {
  1944. const text = this._renderInline(token.tokens, slug);
  1945. html += slug ? text : '<del>' + text + '</del>';
  1946. break;
  1947. }
  1948. default: {
  1949. throw new Error("Unexpected token type '" + token.type + "'.");
  1950. }
  1951. }
  1952. }
  1953. return html;
  1954. }
  1955. _outputLink(match, href, title) {
  1956. title = title ? this._escape(title) : null;
  1957. const text = match[1].replace(/\\([[\]])/g, '$1');
  1958. return match[0].charAt(0) !== '!' ?
  1959. { type: 'link', href: href, title: title, text: text } :
  1960. { type: 'image', href: href, title: title, text: this._escape(text) };
  1961. }
  1962. _splitCells(tableRow, count) {
  1963. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  1964. let escaped = false;
  1965. let position = offset;
  1966. while (--position >= 0 && str[position] === '\\') {
  1967. escaped = !escaped;
  1968. }
  1969. return escaped ? '|' : ' |';
  1970. });
  1971. const cells = row.split(/ \|/);
  1972. if (cells.length > count) {
  1973. cells.splice(count);
  1974. }
  1975. else {
  1976. while (cells.length < count) {
  1977. cells.push('');
  1978. }
  1979. }
  1980. return cells.map((cell) => cell.trim().replace(/\\\|/g, '|'));
  1981. }
  1982. _slug(slugs, value) {
  1983. value = value.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, (_, n) => {
  1984. n = n.toLowerCase();
  1985. if (n === 'colon') {
  1986. return ':';
  1987. }
  1988. if (n.charAt(0) === '#') {
  1989. return String.fromCharCode(n.charAt(1) === 'x' ? parseInt(n.substring(2), 16) : +n.substring(1));
  1990. }
  1991. return '';
  1992. });
  1993. value = value.toLowerCase().trim()
  1994. .replace(/<[!/a-z].*?>/ig, '')
  1995. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  1996. .replace(/\s/g, '-');
  1997. let slug = value;
  1998. let count = 0;
  1999. if (slugs.has(value)) {
  2000. count = slugs.get(value);
  2001. do {
  2002. count++;
  2003. slug = value + '-' + count;
  2004. }
  2005. while (slugs.has(slug));
  2006. }
  2007. slugs.set(value, count);
  2008. slugs.set(slug, 0);
  2009. return slug;
  2010. }
  2011. _encode(text) {
  2012. if (this._escapeTestRegExp.test(text)) {
  2013. return text.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
  2014. }
  2015. return text;
  2016. }
  2017. _escape(text) {
  2018. if (this._escapeTestNoEncodeRegExp.test(text)) {
  2019. return text.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
  2020. }
  2021. return text;
  2022. }
  2023. };
  2024. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  2025. module.exports.Sidebar = sidebar.Sidebar;
  2026. module.exports.ModelSidebar = sidebar.ModelSidebar;
  2027. module.exports.NodeSidebar = sidebar.NodeSidebar;
  2028. module.exports.DocumentationSidebar = sidebar.DocumentationSidebar;
  2029. module.exports.FindSidebar = sidebar.FindSidebar;
  2030. }