pd-to-md.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  7. export function paramsToMd(params: PD.Params) {
  8. return getParams(params, 0);
  9. }
  10. function paramInfo(param: PD.Any, offset: number): string {
  11. switch (param.type) {
  12. case 'value': return 'Value';
  13. case 'boolean': return 'true/false';
  14. case 'number': return 'Numeric value';
  15. case 'converted': return paramInfo(param.converted, offset);
  16. case 'conditioned': return getParams(param.conditionParams, offset);
  17. case 'multi-select': return `Array of ${oToS(param.options)}`;
  18. case 'color': return 'Color as 0xrrggbb';
  19. case 'color-list': return `A list of colors as 0xrrggbb`;
  20. case 'vec3': return `3D vector [x, y, z]`;
  21. case 'file': return `JavaScript File Handle`;
  22. case 'file-list': return `JavaScript FileList Handle`;
  23. case 'select': return `One of ${oToS(param.options)}`;
  24. case 'text': return 'String';
  25. case 'interval': return `Interval [min, max]`;
  26. case 'group': return `Object with:\n${getParams(param.params, offset + 2)}`;
  27. case 'mapped': return `Object { name: string, params: object } where name+params are:\n${getMapped(param, offset + 2)}`;
  28. case 'line-graph': return `A list of 2d vectors [xi, yi][]`;
  29. case 'object-list': return `Array of\n${paramInfo(PD.Group(param.element), offset + 2)}`;
  30. // TODO: support more languages
  31. case 'script': return `An expression in the specified language { language: 'mol-script', expressiong: string }`;
  32. default:
  33. const _: never = param;
  34. console.warn(`${_} has no associated UI component`);
  35. return '';
  36. }
  37. }
  38. function oToS(options: readonly (readonly [string, string] | readonly [string, string, string | undefined])[]) {
  39. return options.map(o => `'${o[0]}'`).join(', ');
  40. }
  41. function offsetS(n: number) {
  42. return new Array(n + 1).join(' ') + '- ';
  43. }
  44. function getMapped(param: PD.Mapped<any>, offset: number) {
  45. let ret = '';
  46. for (const [n] of param.select.options) {
  47. ret += offsetS(offset);
  48. ret += `**${n}**:\n`;
  49. ret += paramInfo(param.map(n), offset + 2);
  50. ret += '\n';
  51. }
  52. return ret;
  53. }
  54. function getParams(params: PD.Params, offset: number) {
  55. let ret = '';
  56. for (const k of Object.keys(params)) {
  57. const param = params[k];
  58. ret += offsetS(offset);
  59. ret += `**${k}**${param.isOptional ? '?:' : ':'} ${paramInfo(param, offset)}`;
  60. // if (param.defaultValue) {
  61. // ret += ` = ${JSON.stringify(param.defaultValue)}`;
  62. // }
  63. if (param.description) {
  64. ret += ` *(${param.description})*`;
  65. }
  66. ret += '\n';
  67. }
  68. return ret;
  69. }