|
| 1 | +import { EditorProvider } from 'sequential-workflow-editor'; |
| 2 | +import { ChownStep, I18nDefinition, definitionModel } from './definition-model'; |
| 3 | +import { Designer, Uid } from 'sequential-workflow-designer'; |
| 4 | +import { defaultI18n } from 'sequential-workflow-editor-model'; |
| 5 | + |
| 6 | +import 'sequential-workflow-designer/css/designer.css'; |
| 7 | +import 'sequential-workflow-designer/css/designer-light.css'; |
| 8 | +import 'sequential-workflow-editor/css/editor.css'; |
| 9 | + |
| 10 | +const designerDict: Record<string, Record<string, string>> = { |
| 11 | + pl: { |
| 12 | + 'controlBar.resetView': 'Resetuj widok', |
| 13 | + 'controlBar.zoomIn': 'Przybliż', |
| 14 | + 'controlBar.zoomOut': 'Oddal', |
| 15 | + 'controlBar.turnOnOffDragAndDrop': 'Włącz/wyłącz przeciąganie i upuszczanie', |
| 16 | + 'controlBar.deleteSelectedStep': 'Usuń wybrany krok', |
| 17 | + 'controlBar.undo': 'Cofnij', |
| 18 | + 'controlBar.redo': 'Dalej', |
| 19 | + 'smartEditor.toggle': 'Zwiń/rozwiń', |
| 20 | + 'toolbox.title': 'Przybornik', |
| 21 | + 'toolbox.search': 'Szukaj', |
| 22 | + 'contextMenu.select': 'Zaznacz', |
| 23 | + 'contextMenu.unselect': 'Odznacz', |
| 24 | + 'contextMenu.delete': 'Usuń', |
| 25 | + 'contextMenu.resetView': 'Resetuj widok', |
| 26 | + 'contextMenu.duplicate': 'Duplikuj', |
| 27 | + |
| 28 | + // steps |
| 29 | + 'toolbox.item.chown.label': 'Uprawnienia' |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const editorDict: Record<string, Record<string, string>> = { |
| 34 | + pl: { |
| 35 | + 'toolbox.defaultGroupName': 'Inne', |
| 36 | + 'stringDictionary.noItems': 'Brak elementów', |
| 37 | + 'stringDictionary.addItem': 'Dodaj element', |
| 38 | + 'stringDictionary.key': 'Klucz', |
| 39 | + 'stringDictionary.value': 'Wartość', |
| 40 | + 'stringDictionary.delete': 'Usuń', |
| 41 | + 'stringDictionary.valueTooShort': 'Wartość musi mieć conajmniej :min znaków', |
| 42 | + 'stringDictionary.duplicatedKey': 'Klucz jest zduplikowany', |
| 43 | + 'stringDictionary.keyIsRequired': 'Klucz jest wymagany', |
| 44 | + |
| 45 | + 'number.valueMustBeNumber': 'Wartość musi być liczbą', |
| 46 | + 'number.valueTooLow': 'Wartość musi być minimum :min.', |
| 47 | + 'number.valueTooHigh': 'Wartość musi być maximum :max.', |
| 48 | + |
| 49 | + 'boolean.false': 'Fałsz', |
| 50 | + 'boolean.true': 'Prawda', |
| 51 | + |
| 52 | + 'string.valueTooShort': 'Wartość musi mieć minimum :min znaków.', |
| 53 | + 'string.valueDoesNotMatchPattern': 'Wartość nie pasuje do oczekiwanego wzorca.', |
| 54 | + |
| 55 | + 'dynamic.string.label': 'Tekst', |
| 56 | + 'dynamic.number.label': 'Liczba', |
| 57 | + |
| 58 | + // root |
| 59 | + 'root.property:properties/timeout': 'Przekroczenie czasu', |
| 60 | + 'root.property:properties/debug': 'Tryb debug', |
| 61 | + |
| 62 | + // steps |
| 63 | + 'step.chown.name': 'Uprawnienia', |
| 64 | + 'step.chown.property:name': 'Nazwa', |
| 65 | + 'step.chown.property:properties/stringOrNumber': 'Tekst lub liczba', |
| 66 | + 'step.chown.property:properties/users': 'Użytkownik' |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +export class App { |
| 71 | + public static create() { |
| 72 | + const placeholder = document.getElementById('designer') as HTMLElement; |
| 73 | + const langInput = document.getElementById('lang') as HTMLInputElement; |
| 74 | + const app = new App(placeholder, langInput.value); |
| 75 | + app.reload(); |
| 76 | + langInput.addEventListener('change', () => { |
| 77 | + app.setLang(langInput.value); |
| 78 | + app.reload(); |
| 79 | + }); |
| 80 | + return app; |
| 81 | + } |
| 82 | + |
| 83 | + private designer: Designer<I18nDefinition> | null = null; |
| 84 | + private definition: I18nDefinition | null = null; |
| 85 | + |
| 86 | + public constructor(private readonly placeholder: HTMLElement, private lang: string) {} |
| 87 | + |
| 88 | + private readonly designerI18n = (key: string, defaultValue: string) => { |
| 89 | + const dict = designerDict[this.lang]; |
| 90 | + if (dict) { |
| 91 | + const translation = dict[key]; |
| 92 | + if (translation) { |
| 93 | + return translation; |
| 94 | + } |
| 95 | + } |
| 96 | + console.log(`<designer>`, key, defaultValue); |
| 97 | + return defaultValue; |
| 98 | + }; |
| 99 | + |
| 100 | + private readonly editorI18n = (key: string, defaultValue: string, replacements?: { [key: string]: string }) => { |
| 101 | + const dict = editorDict[this.lang]; |
| 102 | + if (dict) { |
| 103 | + const translation = dict[key]; |
| 104 | + if (translation) { |
| 105 | + defaultValue = translation; |
| 106 | + } else { |
| 107 | + console.log(`<editor>`, key, defaultValue); |
| 108 | + } |
| 109 | + } |
| 110 | + return defaultI18n(key, defaultValue, replacements); |
| 111 | + }; |
| 112 | + |
| 113 | + public setLang(lang: string) { |
| 114 | + this.lang = lang; |
| 115 | + } |
| 116 | + |
| 117 | + public reload() { |
| 118 | + if (this.designer) { |
| 119 | + this.designer.destroy(); |
| 120 | + } |
| 121 | + |
| 122 | + const editorProvider = EditorProvider.create(definitionModel, { |
| 123 | + uidGenerator: Uid.next, |
| 124 | + i18n: this.editorI18n |
| 125 | + }); |
| 126 | + |
| 127 | + if (!this.definition) { |
| 128 | + this.definition = editorProvider.activateDefinition(); |
| 129 | + const step = editorProvider.activateStep('chown') as ChownStep; |
| 130 | + this.definition.sequence.push(step); |
| 131 | + } |
| 132 | + |
| 133 | + this.designer = Designer.create(this.placeholder, this.definition, { |
| 134 | + controlBar: true, |
| 135 | + editors: { |
| 136 | + rootEditorProvider: editorProvider.createRootEditorProvider(), |
| 137 | + stepEditorProvider: editorProvider.createStepEditorProvider() |
| 138 | + }, |
| 139 | + validator: { |
| 140 | + step: editorProvider.createStepValidator(), |
| 141 | + root: editorProvider.createRootValidator() |
| 142 | + }, |
| 143 | + steps: { |
| 144 | + iconUrlProvider: () => './assets/icon-task.svg' |
| 145 | + }, |
| 146 | + toolbox: { |
| 147 | + groups: editorProvider.getToolboxGroups(), |
| 148 | + labelProvider: editorProvider.createStepLabelProvider() |
| 149 | + }, |
| 150 | + i18n: this.designerI18n |
| 151 | + }); |
| 152 | + this.designer.onDefinitionChanged.subscribe(d => (this.definition = d)); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +document.addEventListener('DOMContentLoaded', App.create, false); |
0 commit comments