Skip to content

Commit

Permalink
feat: parameterized the properties panel subheading getter logic
Browse files Browse the repository at this point in the history
Closes #1290
  • Loading branch information
Skaiir committed Oct 14, 2024
1 parent 05738d9 commit 0024535
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { textToLabel } from './Util';
import { iconsByType } from '../../render/components/icons';
import { getPaletteIcon } from '../palette/components/Palette';

Expand All @@ -8,19 +7,8 @@ export function getPropertiesPanelHeaderProvider(options = {}) {
return {
getElementLabel: (field) => {
const { type } = field;
if (type === 'datetime') {
return field.dateLabel || field.timeLabel;
}
if (type === 'text') {
return textToLabel(field.text);
}
if (type === 'image') {
return field.alt;
}
if (type === 'default') {
return field.id;
}
return field.label;
const fieldDefinition = formFields.get(type).config;
return fieldDefinition.getSubheading ? fieldDefinition.getSubheading(field) : field.label;
},

getElementIcon: (field) => {
Expand Down
15 changes: 0 additions & 15 deletions packages/form-js-editor/src/features/properties-panel/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ export function stopPropagation(listener) {
};
}

export function textToLabel(text) {
if (typeof text != 'string') return null;

for (const line of text.split('\n')) {
const displayLine = line.trim();

// we use the first non-whitespace line in the text as label
if (displayLine !== '') {
return displayLine;
}
}

return null;
}

/**
* @param {string} path
*/
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions packages/form-js-viewer/src/render/components/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ export function gridColumnClasses(formField) {
);
}

export function textToLabel(text) {
if (typeof text != 'string') return null;

for (const line of text.split('\n')) {
const displayLine = line.trim();

// we use the first non-whitespace line in the text as label
if (displayLine !== '') {
return displayLine;
}
}

return null;
}

export function prefixId(id, formId, indexes) {
let result = 'fjs-form';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,7 @@ Datetime.config = {

return { ...defaults, ...options };
},
getSubheading: (field) => {
return field.dateLabel || field.timeLabel;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Default.config = {
components: [],
...options,
}),
getSubheading: (field) => field.id,
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ Image.config = {
name: 'Image view',
group: 'presentation',
create: (options = {}) => options,
getSubheading: (field) => field.alt,
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo } from 'preact/hooks';
import { useDangerousHTMLWrapper, useService, useTemplateEvaluation } from '../../hooks';
import { sanitizeHTML } from '../Sanitizer';

import { formFieldClasses } from '../Util';
import { formFieldClasses, textToLabel } from '../Util';

const type = 'text';

Expand Down Expand Up @@ -66,4 +66,7 @@ Text.config = {
text: '# Text',
...options,
}),
getSubheading: (field) => {
textToLabel(field.text);
},
};
46 changes: 45 additions & 1 deletion packages/form-js-viewer/test/spec/render/components/Util.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formFieldClasses } from '../../../../src/render/components/Util';
import { formFieldClasses, textToLabel } from '../../../../src/render/components/Util';

describe('Util', function () {
describe('#formFieldClasses', function () {
Expand Down Expand Up @@ -26,4 +26,48 @@ describe('Util', function () {
expect(classes).to.equal('fjs-form-field fjs-form-field-button fjs-disabled');
});
});

describe('#textToLabel', function () {
it('should use the first text line as label', function () {
// when
const label = textToLabel(
'Lorem ipsum dolor sit amet\nConsetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
);

// then
expect(label).to.equal('Lorem ipsum dolor sit amet');
});

it('should ignore empty lines', function () {
// when
const label = textToLabel('\n\n\nLorem ipsum dolor sit amet\nConsetetur sadipscing elitr');

// then
expect(label).to.equal('Lorem ipsum dolor sit amet');
});

it('should ignore whitespace lines', function () {
// when
const label = textToLabel('\n \n \nLorem ipsum dolor sit amet\nConsetetur sadipscing elitr');

// then
expect(label).to.equal('Lorem ipsum dolor sit amet');
});

it('should tolerate empty string', function () {
// when
const label = textToLabel('');

// then
expect(label).to.equal(null);
});

it('should tolerate null', function () {
// when
const label = textToLabel(null);

// then
expect(label).to.equal(null);
});
});
});

0 comments on commit 0024535

Please sign in to comment.