Skip to content

Commit

Permalink
Font size util
Browse files Browse the repository at this point in the history
  • Loading branch information
wassgha committed Feb 12, 2020
1 parent c7e1a4e commit b9ac033
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function MultiSelectionMovable({ selectedElements }) {
},
} = useCanvas();
const {
actions: { dataToEditorY, editorToDataX, editorToDataY },
actions: { fontSizeFromEditor, editorToDataX, editorToDataY },
} = useUnits();
const {
actions: { pushTransform },
Expand All @@ -68,8 +68,8 @@ function MultiSelectionMovable({ selectedElements }) {
}, [selectedElements, moveable, nodesById]);

const minMaxFontSize = {
minFontSize: dataToEditorY(MIN_FONT_SIZE),
maxFontSize: dataToEditorY(MAX_FONT_SIZE),
minFontSize: fontSizeFromEditor(MIN_FONT_SIZE),
maxFontSize: fontSizeFromEditor(MAX_FONT_SIZE),
};

// Create targets list including nodes and also necessary attributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ function SingleSelectionMovable({ selectedElement, targetEl, pushEvent }) {
},
} = useCanvas();
const {
actions: { getBox, dataToEditorY, editorToDataX, editorToDataY },
actions: { getBox, fontSizeFromEditor, editorToDataX, editorToDataY },
} = useUnits();
const {
actions: { pushTransform },
} = useTransform();

const minMaxFontSize = {
minFontSize: dataToEditorY(MIN_FONT_SIZE),
maxFontSize: dataToEditorY(MAX_FONT_SIZE),
minFontSize: fontSizeFromEditor(MIN_FONT_SIZE),
maxFontSize: fontSizeFromEditor(MAX_FONT_SIZE),
};

const otherNodes = Object.values(
Expand Down
2 changes: 2 additions & 0 deletions assets/src/edit-story/components/library/textLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { fontSizeFromData } from '../../units/dimensions';
import { Section, MainButton, Title, SearchInput, Header } from './common';
import { FontPreview } from './text';

Expand Down Expand Up @@ -95,6 +96,7 @@ function TextLibrary({ onInsert }) {
y: 5,
rotationAngle: 0,
...preset,
fontSize: fontSizeFromData(preset.fontSize),

This comment has been minimized.

Copy link
@dvoytenko

dvoytenko Feb 14, 2020

Contributor

Preset should be in exactly the same measurement system as data, imho.

})
}
/>
Expand Down
26 changes: 25 additions & 1 deletion assets/src/edit-story/units/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
/**
* Internal dependencies
*/
import { PAGE_WIDTH, PAGE_HEIGHT } from '../constants';
import {
PAGE_WIDTH,
PAGE_HEIGHT,
DEFAULT_EDITOR_PAGE_HEIGHT,
} from '../constants';

/**
* Rounds the pixel value to the max allowed precision in the "data" space.
Expand Down Expand Up @@ -87,6 +91,26 @@ export function editorToDataY(y, pageHeight) {
return dataPixels((y * PAGE_HEIGHT) / pageHeight);
}

/**
* Converts a "data" font size value to the value in the "editor" space
*
* @param {number} size The value to be converted.
* @return {number} The value in the "editor" space.
*/
export function fontSizeFromData(size) {
return editorPixels((size * PAGE_HEIGHT) / DEFAULT_EDITOR_PAGE_HEIGHT);
}

/**
* Converts a "editor" font size value to the value in the "data" space
*
* @param {number} size The value to be converted.
* @return {number} The value in the "editor" space.
*/
export function fontSizeFromEditor(size) {
return editorPixels((size * DEFAULT_EDITOR_PAGE_HEIGHT) / PAGE_HEIGHT);
}

/**
* Converts the element's position, width, and rotation) to the "box" in the
* "editor" coordinate space.
Expand Down
4 changes: 4 additions & 0 deletions assets/src/edit-story/units/unitsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
dataToEditorY,
editorToDataX,
editorToDataY,
fontSizeFromData,
fontSizeFromEditor,
getBox,
} from './dimensions';

Expand All @@ -44,6 +46,8 @@ function UnitsProvider({ pageSize, children }) {
dataToEditorY: (y) => dataToEditorY(y, pageHeight),
editorToDataX: (x) => editorToDataX(x, pageWidth),
editorToDataY: (y) => editorToDataY(y, pageHeight),
fontSizeFromData: (size) => fontSizeFromData(size),
fontSizeFromEditor: (size) => fontSizeFromEditor(size),

This comment has been minimized.

Copy link
@barklund

barklund Feb 12, 2020

Contributor

The functions only takes this single argument, no?

Could be replaced with simply:

        fontSizeFromData,
        fontSizeFromEditor,

This comment has been minimized.

Copy link
@dvoytenko

dvoytenko Feb 13, 2020

Contributor

Hmm. There's no real difference between fontSize and y. Do we really need these?

This comment has been minimized.

Copy link
@wassgha

wassgha Feb 13, 2020

Author Contributor

Not needed, for now just a shortcut utility but I'm expecting it to change in the future, right?

This comment has been minimized.

Copy link
@dvoytenko

dvoytenko Feb 14, 2020

Contributor

Just trying to clarify exactly what these are: fontSize is exactly the same as the Y dimension in every respect. So from that point of view we can just use dataToEditorY and editorToDataY.

This comment has been minimized.

Copy link
@dvoytenko

dvoytenko Feb 14, 2020

Contributor

I think this is really just a naming issue. fontSizeFromData is slightly confusing. It's more of a "default editor to data" and "data to default editor". But at this point I'm wondering if it'd be just better for us to drop this util and use "data pixels" instead for library. We could make it a bit saner if we put all constants that are used in the library into a single file. That way if we decide to change the "data pixel" space, we'd only need to change it in a single place.

getBox: (element) => getBox(element, pageWidth, pageHeight),
},
}),
Expand Down

0 comments on commit b9ac033

Please sign in to comment.