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

Support justified text align inside text blocks #2179

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions entry_types/scrolled/config/locales/new/align_justified.de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
de:
pageflow_scrolled:
editor:
content_elements:
textBlock:
attributes:
textAlign:
label: "Textausrichtung"
values:
ragged: "Flattersatz"
justify: "Blocksatz"
11 changes: 11 additions & 0 deletions entry_types/scrolled/config/locales/new/align_justified.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en:
pageflow_scrolled:
editor:
content_elements:
textBlock:
attributes:
textAlign:
label: "Text align"
values:
ragged: "Ragged"
justify: "Justified"
30 changes: 30 additions & 0 deletions entry_types/scrolled/package/spec/frontend/EditableText-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,36 @@ describe('EditableText', () => {
'color: rgb(0, 0, 0);');
});

it('renders text align', () => {
const value = [{
type: 'paragraph',
textAlign: 'justify',
children: [
{text: 'Some text'}
]
}];

const {container} = render(<EditableText value={value} />);

expect(container.querySelector('p[style]')).toHaveStyle('text-align: justify;');
});

it('can render both color and text align', () => {
const value = [{
type: 'paragraph',
textAlign: 'justify',
color: '#000',
children: [
{text: 'Some text'}
]
}];

const {container} = render(<EditableText value={value} />);

expect(container.querySelector('p[style]')).toHaveStyle('text-align: justify;');
expect(container.querySelector('p[style]')).toHaveStyle('color: rgb(0, 0, 0);');
});

it('uses body scaleCategory by default', () => {
const value = [{
type: 'paragraph',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
applyTypograpyhVariant,
applyColor,
applyTextAlign,
isBlockActive,
toggleBlock,
withBlockNormalization
Expand Down Expand Up @@ -423,6 +424,119 @@ describe('applyColor', () => {
});
});

describe('applyTextAlign', () => {
it('sets color property deeply in lists', () => {
const editor = (
<editor>
<bulletedList>
<listItem>
Item 1
<cursor />
</listItem>
<listItem>
Item 1
</listItem>
</bulletedList>
</editor>
);

applyTextAlign(editor, 'justify');

const output = (
<editor>
<bulletedList textAlign="justify">
<listItem textAlign="justify">
Item 1
</listItem>
<listItem textAlign="justify">
Item 1
</listItem>
</bulletedList>
</editor>
);
expect(editor.children).toEqual(output.children);
});

it('sets textAlign property of multiple elements', () => {
const editor = (
<editor>
<paragraph>
<anchor />
Text
</paragraph>
<paragraph>
More Text
<focus />
</paragraph>
<paragraph>
Other Text
</paragraph>
</editor>
);

applyTextAlign(editor, 'justify');

const output = (
<editor>
<paragraph textAlign="justify">
Text
</paragraph>
<paragraph textAlign="justify">
More Text
</paragraph>
<paragraph>
Other Text
</paragraph>
</editor>
);
expect(editor.children).toEqual(output.children);
});

it('unsets textAlign property if set to ragged', () => {
const editor = (
<editor>
<paragraph textAlign="justify">
<cursor />
Text
</paragraph>
</editor>
);

applyTextAlign(editor, 'ragged');

const output = (
<editor>
<paragraph>
Text
</paragraph>
</editor>
);
expect(editor.children).toEqual(output.children);
});

it('unsets textAlign property if blank', () => {
const editor = (
<editor>
<paragraph textAlign="justify">
<cursor />
Text
</paragraph>
</editor>
);

applyTextAlign(editor, undefined);

const output = (
<editor>
<paragraph>
Text
</paragraph>
</editor>
);
expect(editor.children).toEqual(output.children);
});
});

describe('withBlockNormalization', () => {
describe('with onlyParagraphs false', () => {
it('is no-op by default', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import I18n from 'i18n-js';
import {utils} from 'pageflow-scrolled/frontend';
import {editor} from 'pageflow-scrolled/editor';
import {InfoBoxView} from 'pageflow/editor';
import {SeparatorView} from 'pageflow/ui'
import {SeparatorView, SelectInputView} from 'pageflow/ui'

import pictogram from './pictogram.svg';

Expand Down Expand Up @@ -51,6 +51,10 @@ editor.contentElementTypes.register('textBlock', {
model: modelDelegator,
propertyName: 'color'
});
this.input('textAlign', SelectInputView, {
model: contentElement.transientState,
values: ['ragged', 'justify']
});

this.view(SeparatorView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ storiesOfContentElement(module, {
]
}
},
{
name: 'Text align justify',
configuration: {
value: [
{
type: 'heading',
textAlign: 'justify',
children: [
{text: 'Heading'}
]
},
{
type: 'paragraph',
textAlign: 'justify',
children: [
{text: 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr.'},
]
}
]
}
},
{
name: 'With theme link color',
configuration: linkExampleConfiguration,
Expand Down
7 changes: 4 additions & 3 deletions entry_types/scrolled/package/src/frontend/EditableText.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export function renderElement({attributes, children, element}) {
['typography-textBlock',
camelize(element.type),
element.variant].join('-');

const styles = element.color &&
{color: paletteColor(element.color)};
const styles = {
...(element.color && {color: paletteColor(element.color)}),
...(element.textAlign && {textAlign: element.textAlign})
};

switch (element.type) {
case 'block-quote':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export function Selection(props) {
editableTextIsSingleBlock: editor.children.length <= 1,
exampleNode: getUniformSelectedNode(editor, 'type'),
typographyVariant: getUniformSelectedNode(editor, 'variant')?.variant,
color: getUniformSelectedNode(editor, 'color')?.color
color: getUniformSelectedNode(editor, 'color')?.color,
textAlign: getUniformSelectedNode(editor, 'textAlign')?.textAlign,
});

boundsRef.current = {start, end};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export function applyColor(editor, color) {
applyProperties(editor, {color});
}

export function applyTextAlign(editor, textAlign) {
applyProperties(editor, {textAlign: textAlign === 'justify' ? 'justify' : undefined});
}

function applyProperties(editor, properties) {
Transforms.setNodes(editor, properties, {mode: 'highest'});
applyPropertiesToListItems(editor, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Selection} from './Selection';
import {DropTargets} from './DropTargets';
import {LinkTooltipProvider} from '../LinkTooltip';
import {
applyTextAlign,
applyTypograpyhVariant,
applyColor,
withBlockNormalization
Expand Down Expand Up @@ -99,6 +100,9 @@ export const EditableText = React.memo(function EditableText({
if ('color' in command.payload) {
applyColor(editor, command.payload.color);
}
if ('textAlign' in command.payload) {
applyTextAlign(editor, command.payload.textAlign);
}
}
});

Expand Down
Loading