ToolkitEditor.svelte 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <script>
  2. import { getContext, createEventDispatcher } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import CodeEditor from './CodeEditor.svelte';
  5. import { goto } from '$app/navigation';
  6. const dispatch = createEventDispatcher();
  7. let formElement = null;
  8. let loading = false;
  9. export let edit = false;
  10. export let id = '';
  11. export let name = '';
  12. export let meta = {
  13. description: ''
  14. };
  15. export let content = '';
  16. $: if (name && !edit) {
  17. id = name.replace(/\s+/g, '_').toLowerCase();
  18. }
  19. let codeEditor;
  20. const saveHandler = async () => {
  21. loading = true;
  22. dispatch('save', {
  23. id,
  24. name,
  25. meta,
  26. content
  27. });
  28. };
  29. const submitHandler = async () => {
  30. if (codeEditor) {
  31. const res = await codeEditor.formatHandler();
  32. if (res) {
  33. console.log('Code formatted successfully');
  34. saveHandler();
  35. }
  36. }
  37. };
  38. </script>
  39. <div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
  40. <div class="mx-auto w-full md:px-0 h-full">
  41. <form
  42. bind:this={formElement}
  43. class=" flex flex-col max-h-[100dvh] h-full"
  44. on:submit|preventDefault={() => {
  45. submitHandler();
  46. }}
  47. >
  48. <div class="mb-2.5">
  49. <button
  50. class="flex space-x-1"
  51. on:click={() => {
  52. goto('/workspace/tools');
  53. }}
  54. type="button"
  55. >
  56. <div class=" self-center">
  57. <svg
  58. xmlns="http://www.w3.org/2000/svg"
  59. viewBox="0 0 20 20"
  60. fill="currentColor"
  61. class="w-4 h-4"
  62. >
  63. <path
  64. fill-rule="evenodd"
  65. d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
  66. clip-rule="evenodd"
  67. />
  68. </svg>
  69. </div>
  70. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  71. </button>
  72. </div>
  73. <div class="flex flex-col flex-1 overflow-auto h-0 rounded-lg">
  74. <div class="w-full mb-2 flex flex-col gap-1.5">
  75. <div class="flex gap-2 w-full">
  76. <input
  77. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  78. type="text"
  79. placeholder="Toolkit Name (e.g. My ToolKit)"
  80. bind:value={name}
  81. required
  82. />
  83. <input
  84. class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  85. type="text"
  86. placeholder="Toolkit ID (e.g. my_toolkit)"
  87. bind:value={id}
  88. required
  89. disabled={edit}
  90. />
  91. </div>
  92. <input
  93. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  94. type="text"
  95. placeholder="Toolkit Description (e.g. A toolkit for performing various operations)"
  96. bind:value={meta.description}
  97. required
  98. />
  99. </div>
  100. <div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
  101. <CodeEditor
  102. bind:value={content}
  103. bind:this={codeEditor}
  104. on:save={() => {
  105. if (formElement) {
  106. formElement.requestSubmit();
  107. }
  108. }}
  109. />
  110. </div>
  111. <div class="pb-3 flex justify-end">
  112. <button
  113. class="px-3 py-1.5 text-sm font-medium bg-emerald-600 hover:bg-emerald-700 text-gray-50 transition rounded-lg"
  114. >
  115. {$i18n.t('Save')}
  116. </button>
  117. </div>
  118. </div>
  119. </form>
  120. </div>
  121. </div>