view-sidebar.js 92 KB

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