Skip to content

Commit

Permalink
feat: add parse alignment for p tag
Browse files Browse the repository at this point in the history
  • Loading branch information
LanceRabbit committed Aug 17, 2022
1 parent 655a10d commit cb410f4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/edjsHTML.browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/edjsHTML.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/edjsHTML.node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build/transforms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export declare type block = {
embed?: string;
width?: number;
height?: number;
alignment?: "left" | "right" | "center" | "justify";
align?: "left" | "right" | "center" | "justify";
};
};
declare const transforms: transforms;
Expand Down
12 changes: 11 additions & 1 deletion src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type ListItem = {
items: Array<ListItem>;
};

const alignType = ["left", "right", "center", "justify"]

export type block = {
type: string;
data: {
Expand All @@ -36,6 +38,8 @@ export type block = {
embed?: string;
width?: number;
height?: number;
alignment?: "left" | "right" | "center" | "justify";
align?: "left" | "right" | "center" | "justify";
};
};

Expand All @@ -49,7 +53,13 @@ const transforms: transforms = {
},

paragraph: ({ data }) => {
return `<p>${data.text}</p>`;
const paragraphAlign = data.alignment || data.align;

if (typeof paragraphAlign !== 'undefined' && alignType.includes(paragraphAlign)) {
return `<p style="text-align:${paragraphAlign};">${data.text}</p>`;
} else {
return `<p>${data.text}</p>`
}
},

list: ({ data }) => {
Expand Down
15 changes: 10 additions & 5 deletions test/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
{
"type": "paragraph",
"data": {
"text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
"text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text.",
"alignment": "justify"
}
},
{
Expand Down Expand Up @@ -42,13 +43,15 @@
{
"type": "paragraph",
"data": {
"text": "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core."
"text": "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core.",
"alignment": "right"
}
},
{
"type": "paragraph",
"data": {
"text": "There are dozens of <a href=\"https://github.com/editor-js\">ready-to-use Blocks</a> and the <a href=\"https://editorjs.io/creating-a-block-tool\">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games."
"text": "There are dozens of <a href=\"https://github.com/editor-js\">ready-to-use Blocks</a> and the <a href=\"https://editorjs.io/creating-a-block-tool\">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.",
"alignment": "left"
}
},
{
Expand All @@ -61,7 +64,8 @@
{
"type": "paragraph",
"data": {
"text": "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"
"text": "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below",
"alignment": "center"
}
},
{
Expand All @@ -73,7 +77,8 @@
{
"type": "paragraph",
"data": {
"text": "Clean data is useful to sanitize, validate and process on the backend."
"text": "Clean data is useful to sanitize, validate and process on the backend.",
"alignment": "center"
}
},
{
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ console.log(customParser.validate(data));

// test for issue #21
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar"}}) === '<p>foo bar</p>');

// test for issue #39
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", alignment: "right"}}) === '<p style="text-align:right;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", alignment: "justify"}}) === '<p style="text-align:justify;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", alignment: "center"}}) === '<p style="text-align:center;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", alignment: "left"}}) === '<p style="text-align:left;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", alignment: "wrong type"}}) === '<p>foo bar</p>');

console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", align: "right"}}) === '<p style="text-align:right;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", align: "justify"}}) === '<p style="text-align:justify;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", align: "center"}}) === '<p style="text-align:center;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", align: "left"}}) === '<p style="text-align:left;">foo bar</p>');
console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar", align: "wrong type"}}) === '<p>foo bar</p>');



0 comments on commit cb410f4

Please sign in to comment.