pd-to-md.ts 2.8 KB

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