Skip to content

Commit

Permalink
feat: added paragraph spacing support (#73)
Browse files Browse the repository at this point in the history
Created ParagraphStyle model and used it in StringAttribute and TextStyle. Added paragraph spacing property to it.
  • Loading branch information
L2jLiga authored and dbanksdesign committed Dec 5, 2019
1 parent bf3ac80 commit 28c7069
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 33 deletions.
39 changes: 39 additions & 0 deletions models/ParagraphStyle/ParagraphStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const { textAlignmentMap } = require('../../utils/maps');

class ParagraphStyle {
static get Model() {
return {
_class: 'paragraphStyle',
alignment: 0,
};
}

constructor(args, json) {
if (json) {
Object.assign(this, json);
} else {
Object.assign(this, ParagraphStyle.Model);
if (args.alignment) this.alignment = textAlignmentMap[args.alignment];
if (args.lineHeight) {
this.maximumLineHeight = args.lineHeight;
this.minimumLineHeight = args.lineHeight;
}
if (args.paragraphSpacing) this.paragraphSpacing = args.paragraphSpacing;
}
}
}

module.exports = ParagraphStyle;
31 changes: 31 additions & 0 deletions models/ParagraphStyle/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

declare const enum Alignment {
LEFT = 0,
RIGHT = 1,
CENTER = 2,
JUSTIFY = 3,
}

declare class ParagraphStyle {
_class: 'paragraphStyle';
alignment: Alignment;
minimumLineHeight: number;
maximumLineHeight: number;
paragraphSpacing: number;

constructor(args?: any, json?: any);
}

export = ParagraphStyle;
14 changes: 14 additions & 0 deletions models/ParagraphStyle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

module.exports = require('./ParagraphStyle');
15 changes: 4 additions & 11 deletions models/StringAttribute/StringAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

const Color = require('../Color');
const { textAlignmentMap, verticalAlignmentMap, textTransformMap } = require('../../utils/maps');
const ParagraphStyle = require('../ParagraphStyle');
const { verticalAlignmentMap, textTransformMap } = require('../../utils/maps');

class StringAttribute {
static get Model() {
Expand All @@ -33,10 +34,7 @@ class StringAttribute {
textStyleVerticalAlignmentKey: 0,
underlineStyle: 0,
strikethroughStyle: 0,
paragraphStyle: {
_class: 'paragraphStyle',
alignment: 0,
},
paragraphStyle: ParagraphStyle.Model,
},
};
}
Expand All @@ -62,12 +60,7 @@ class StringAttribute {
underlineStyle: args.underline ? 1 : 0,
strikethroughStyle: args.strikethrough ? 1 : 0,
kerning: args.kerning || undefined,
paragraphStyle: {
_class: 'paragraphStyle',
alignment: textAlignmentMap[args.alignment || 'left'],
...(args.lineHeight && { maximumLineHeight: args.lineHeight }),
...(args.lineHeight && { minimumLineHeight: args.lineHeight }),
},
paragraphStyle: new ParagraphStyle(args),
},
});
}
Expand Down
7 changes: 2 additions & 5 deletions models/StringAttribute/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Color from '../Color';
// import { textAlignmentMap, verticalAlignmentMap } from '../../utils/maps';
import ParagraphStyle = require('../ParagraphStyle');

declare class StringAttribute {
_class: 'stringAttribute';
Expand All @@ -17,10 +17,7 @@ declare class StringAttribute {
textStyleVerticalAlignmentKey: number;
underlineStyle: number;
strikethroughStyle: number;
paragraphStyle: {
_class: 'paragraphStyle';
alignment: number;
};
paragraphStyle: ParagraphStyle;
};
constructor(args?: any, json?: any);
}
Expand Down
15 changes: 4 additions & 11 deletions models/TextStyle/TextStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

const Color = require('../Color');
const { textAlignmentMap, verticalAlignmentMap } = require('../../utils/maps');
const ParagraphStyle = require('../ParagraphStyle');
const { verticalAlignmentMap } = require('../../utils/maps');

/**
* TextStyle
Expand All @@ -34,12 +35,7 @@ class TextStyle {
textStyleVerticalAlignmentKey: 0,
underlineStyle: 0,
strikethroughStyle: 0,
paragraphStyle: {
_class: 'paragraphStyle',
alignment: 0,
minimumLineHeight: 43,
maximumLineHeight: 43,
},
paragraphStyle: ParagraphStyle.Model,
},
verticalAlignment: 0,
kerning: 0,
Expand Down Expand Up @@ -73,10 +69,7 @@ class TextStyle {
},
MSAttributedStringColorAttribute: new Color(args.color),
textStyleVerticalAlignmentKey: 2,
paragraphStyle: {
_class: 'paragraphStyle',
alignment: textAlignmentMap[args.alignment || 'left'],
},
paragraphStyle: new ParagraphStyle(args),
},
});
if (args.lineHeight) {
Expand Down
8 changes: 2 additions & 6 deletions models/TextStyle/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Color from '../Color';
import ParagraphStyle = require('../ParagraphStyle');
// import { textAlignmentMap, verticalAlignmentMap } from '../../utils/maps';

declare class TextStyle {
Expand All @@ -16,12 +17,7 @@ declare class TextStyle {
underlineStyle: number;
strikethroughStyle: number;
kerning: number;
paragraphStyle: {
_class: 'paragraphStyle';
alignment: number;
minimumLineHeight: number;
maximumLineHeight: number;
};
paragraphStyle: ParagraphStyle;
};
verticalAlignment: number;

Expand Down
2 changes: 2 additions & 0 deletions models/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import _Layer from './Layer';
import Meta from './Meta';
import Oval from './Oval';
import Page from './Page';
import ParagraphStyle from './ParagraphStyle';
import Rect from './Rect';
import Rectangle from './Rectangle';
import RulerData from './RulerData';
Expand Down Expand Up @@ -54,6 +55,7 @@ export {
Meta,
Oval,
Page,
ParagraphStyle,
Rect,
Rectangle,
RulerData,
Expand Down
1 change: 1 addition & 0 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
Meta: require('./Meta'),
Oval: require('./Oval'),
Page: require('./Page'),
ParagraphStyle: require('./ParagraphStyle'),
Rect: require('./Rect'),
Rectangle: require('./Rectangle'),
RulerData: require('./RulerData'),
Expand Down

0 comments on commit 28c7069

Please sign in to comment.