view-sidebar.js 94 KB

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