pd-to-md.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 'mat4': return `4x4 transformation matrix`;
  22. case 'url': return `URL couple with unique identifier`;
  23. case 'file': return `JavaScript File Handle`;
  24. case 'file-list': return `JavaScript FileList Handle`;
  25. case 'select': return `One of ${oToS(param.options)}`;
  26. case 'text': return 'String';
  27. case 'interval': return `Interval [min, max]`;
  28. case 'group': return `Object with:\n${getParams(param.params, offset + 2)}`;
  29. case 'mapped': return `Object { name: string, params: object } where name+params are:\n${getMapped(param, offset + 2)}`;
  30. case 'line-graph': return `A list of 2d vectors [xi, yi][]`;
  31. case 'object-list': return `Array of\n${paramInfo(PD.Group(param.element), offset + 2)}`;
  32. // TODO: support more languages
  33. case 'script': return `An expression in the specified language { language: 'mol-script', expressiong: string }`;
  34. default:
  35. const _: never = param;
  36. console.warn(`${_} has no associated UI component`);
  37. return '';
  38. }
  39. }
  40. function oToS(options: readonly (readonly [string, string] | readonly [string, string, string | undefined])[]) {
  41. return options.map(o => `'${o[0]}'`).join(', ');
  42. }
  43. function offsetS(n: number) {
  44. return new Array(n + 1).join(' ') + '- ';
  45. }
  46. function getMapped(param: PD.Mapped<any>, offset: number) {
  47. let ret = '';
  48. for (const [n] of param.select.options) {
  49. ret += offsetS(offset);
  50. ret += `**${n}**:\n`;
  51. ret += paramInfo(param.map(n), offset + 2);
  52. ret += '\n';
  53. }
  54. return ret;
  55. }
  56. function getParams(params: PD.Params, offset: number) {
  57. let ret = '';
  58. for (const k of Object.keys(params)) {
  59. const param = params[k];
  60. ret += offsetS(offset);
  61. ret += `**${k}**${param.isOptional ? '?:' : ':'} ${paramInfo(param, offset)}`;
  62. // if (param.defaultValue) {
  63. // ret += ` = ${JSON.stringify(param.defaultValue)}`;
  64. // }
  65. if (param.description) {
  66. ret += ` *(${param.description})*`;
  67. }
  68. ret += '\n';
  69. }
  70. return ret;
  71. }