Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): Adjust HTML editor component for use in params #5285

Merged
merged 5 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions packages/editor-ui/src/components/HtmlEditor/HtmlEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ export default mixins(expressionManager).extend({
props: {
html: {
type: String,
required: true,
},
isReadOnly: {
type: Boolean,
default: false,
},
rows: {
type: Number,
default: -1,
},
disableExpressionColoring: {
type: Boolean,
default: false,
},
},
data() {
return {
Expand Down Expand Up @@ -72,8 +81,8 @@ export default mixins(expressionManager).extend({
EditorView.updateListener.of((viewUpdate: ViewUpdate) => {
if (!viewUpdate.docChanged) return;

highlighter.removeColor(this.editor, this.htmlSegments);
highlighter.addColor(this.editor, this.resolvableSegments);
this.getHighlighter()?.removeColor(this.editor, this.htmlSegments);
this.getHighlighter()?.addColor(this.editor, this.resolvableSegments);

this.$emit('valueChanged', this.doc);
}),
Expand Down Expand Up @@ -144,7 +153,31 @@ export default mixins(expressionManager).extend({
return root;
},

isMissingHtmlTags() {
const zerothSection = this.sections.at(0);

return (
!zerothSection?.content.trim().startsWith('<html') &&
!zerothSection?.content.trim().endsWith('</html>')
);
},

format() {
if (this.sections.length === 1 && this.isMissingHtmlTags()) {
const zerothSection = this.sections.at(0) as Section;

const formatted = prettier
.format(zerothSection.content, {
parser: 'html',
plugins: [htmlParser],
})
.trim();

return this.editor.dispatch({
changes: { from: 0, to: this.doc.length, insert: formatted },
});
}

const formatted = [];

for (const { kind, content } of this.sections) {
Expand Down Expand Up @@ -185,20 +218,34 @@ export default mixins(expressionManager).extend({
}
}

if (formatted.length === 0) return;

this.editor.dispatch({
changes: { from: 0, to: this.doc.length, insert: formatted.join('\n\n') },
});
},

getHighlighter() {
if (this.disableExpressionColoring) return;

return highlighter;
},
},

mounted() {
htmlEditorEventBus.$on('format-html', this.format);

const state = EditorState.create({ doc: this.html, extensions: this.extensions });
let doc = this.html;

if (this.html === '' && this.rows > 0) {
doc = '\n'.repeat(this.rows - 1);
}

const state = EditorState.create({ doc, extensions: this.extensions });

this.editor = new EditorView({ parent: this.root(), state });

highlighter.addColor(this.editor, this.resolvableSegments);
this.getHighlighter()?.addColor(this.editor, this.resolvableSegments);
},

destroyed() {
Expand Down
5 changes: 5 additions & 0 deletions packages/editor-ui/src/components/HtmlEditor/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const theme = [
'.cm-cursor, .cm-dropCursor': {
borderLeftColor: 'var(--color-code-caret)',
},
'&.cm-editor.cm-focused': {
outline: '0',
},
'&.cm-focused .cm-selectionBackgroundm .cm-selectionBackground, .cm-content ::selection': {
backgroundColor: 'var(--color-code-selection)',
},
Expand All @@ -30,6 +33,8 @@ export const theme = [
'.cm-gutters': {
backgroundColor: 'var(--color-code-gutterBackground)',
color: 'var(--color-code-gutterForeground)',
borderTopLeftRadius: 'var(--border-radius-base)',
borderBottomLeftRadius: 'var(--border-radius-base)',
},
'.cm-scroller': {
overflow: 'auto',
Expand Down
8 changes: 5 additions & 3 deletions packages/editor-ui/src/components/ParameterInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@
/>

<html-editor
v-else-if="getArgument('editor') === 'htmlEditor' && isHtmlNode(node)"
v-else-if="getArgument('editor') === 'htmlEditor'"
:html="node.parameters.html"
:isReadOnly="isReadOnly"
:rows="getArgument('rows')"
:disableExpressionColoring="!isHtmlNode(node)"
@valueChanged="valueChangedDebounced"
/>

Expand Down Expand Up @@ -352,8 +354,8 @@ import { workflowHelpers } from '@/mixins/workflowHelpers';
import { hasExpressionMapping, isValueExpression, isResourceLocatorValue } from '@/utils';

import mixins from 'vue-typed-mixins';
import { CUSTOM_API_CALL_KEY } from '@/constants';
import { CODE_NODE_TYPE, HTML_NODE_TYPE } from '@/constants';
import { CUSTOM_API_CALL_KEY, HTML_NODE_TYPE } from '@/constants';
import { CODE_NODE_TYPE } from '@/constants';
import { PropType } from 'vue';
import { debounceHelper } from '@/mixins/debounce';
import { mapStores } from 'pinia';
Expand Down
11 changes: 5 additions & 6 deletions packages/editor-ui/src/components/ParameterOptions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Vue, { PropType } from 'vue';
import { isValueExpression, isResourceLocatorValue } from '@/utils';
import { useNDVStore } from '@/stores/ndv';
import { mapStores } from 'pinia';
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
import { HTML_NODE_TYPE } from '@/constants';

export default Vue.extend({
name: 'parameter-options',
Expand Down Expand Up @@ -61,6 +60,9 @@ export default Vue.extend({
isValueExpression(): boolean {
return isValueExpression(this.parameter, this.value);
},
isHtmlEditor(): boolean {
return this.getArgument('editor') === 'htmlEditor';
},
shouldShowOptions(): boolean {
if (this.isReadOnly === true) {
return false;
Expand Down Expand Up @@ -95,13 +97,10 @@ export default Vue.extend({
return !!this.getArgument('loadOptionsMethod') || !!this.getArgument('loadOptions');
},
actions(): Array<{ label: string; value: string; disabled?: boolean }> {
if (
this.ndvStore.activeNode?.type === HTML_NODE_TYPE &&
this.ndvStore.activeNode?.parameters.operation === 'generateHtmlTemplate'
) {
if (this.isHtmlEditor && !this.isValueExpression) {
return [
{
label: 'Format HTML',
label: this.$locale.baseText('parameterInput.formatHtml'),
value: 'formatHtml',
},
];
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/src/plugins/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@
"parameterInput.error": "ERROR",
"parameterInput.expression": "Expression",
"parameterInput.fixed": "Fixed",
"parameterInput.formatHtml": "Format HTML",
"parameterInput.issues": "Issues",
"parameterInput.loadingOptions": "Loading options...",
"parameterInput.openEditWindow": "Open Edit Window",
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/nodes/Mailgun/Mailgun.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class Mailgun implements INodeType {
type: 'string',
typeOptions: {
rows: 5,
editor: 'htmlEditor',
},
default: '',
description: 'HTML text message of email',
Expand Down