Skip to content

Commit

Permalink
fix: prefer text-align instead of align
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Dec 24, 2020
1 parent b1e85bb commit ee03ee7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/lib/schema/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DOMOutputSpec, Node as ProsemirrorNode, NodeSpec } from 'prosemirror-model';
import * as sl from 'prosemirror-schema-list';

import toStyleString from '../utils/toStyleString';

type GetAttrsSpec = { [key: string]: any };

const doc: NodeSpec = {
Expand All @@ -26,15 +28,24 @@ const paragraph: NodeSpec = {
{
tag: 'p',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
align: dom.getAttribute('align') || null
align
};
}
}
],
toDOM(node): DOMOutputSpec {
const { align } = node.attrs;
return ['p', { align }, 0];

const styles: Partial<CSSStyleDeclaration> = {
textAlign: align !== 'left' ? align : null
};
const style = toStyleString(styles) || null;

return ['p', { style }, 0];
}
};

Expand Down Expand Up @@ -77,61 +88,85 @@ const heading: NodeSpec = {
{
tag: 'h1',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 1,
align: dom.getAttribute('align') || null
align
};
}
},
{
tag: 'h2',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 2,
align: dom.getAttribute('align') || null
align
};
}
},
{
tag: 'h3',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 3,
align: dom.getAttribute('align') || null
align
};
}
},
{
tag: 'h4',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 4,
align: dom.getAttribute('align') || null
align
};
}
},
{
tag: 'h5',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 5,
align: dom.getAttribute('align') || null
align
};
}
},
{
tag: 'h6',
getAttrs(dom: HTMLElement): GetAttrsSpec {
const { textAlign } = dom.style;
const align = dom.getAttribute('align') || textAlign || null;

return {
level: 6,
align: dom.getAttribute('align') || null
align
};
}
},
],
toDOM(node): DOMOutputSpec {
const { align } = node.attrs;
return ['h' + node.attrs.level, { align }, 0];
const { level, align } = node.attrs;

const styles: Partial<CSSStyleDeclaration> = {
textAlign: align !== 'left' ? align : null
};
const style = toStyleString(styles) || null;

return ['h' + level, { style }, 0];
}
};

Expand Down
22 changes: 22 additions & 0 deletions src/lib/utils/toStyleString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const camelToDashed = (str: string): string => {
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
};

const cleanObject = (obj: { [key: string]: any }): { [key: string]: any } => {
const cleanObj = {};

Object.keys(obj).forEach((prop) => {
if (obj[prop]) {
cleanObj[prop] = obj[prop];
}
});

return cleanObj;
};

const toStyleString = (obj: Partial<CSSStyleDeclaration>): string => {
const styles = cleanObject(obj);
return Object.entries(styles).map(([k, v]) => `${camelToDashed(k)}:${v}`).join(';');
};

export default toStyleString;

0 comments on commit ee03ee7

Please sign in to comment.