Skip to content

Commit ad2a7b7

Browse files
Merge pull request #1305 from MetRonnie/vue3-font-size
Fix font size setting post Vue 3
2 parents c34dead + df3d0a2 commit ad2a7b7

File tree

5 files changed

+62
-67
lines changed

5 files changed

+62
-67
lines changed

src/components/cylc/tree/TreeItem.vue

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4141
<!-- the node value -->
4242
<!-- TODO: revisit these values that can be replaced by constants later (and in other components too). -->
4343
<slot name="cyclepoint" v-if="node.type === 'cycle'">
44-
<div :class="getNodeDataClass()" @click="nodeClicked">
44+
<div :class="nodeDataClass" @click="nodeClicked">
4545
<!-- NOTE: cycle point nodes don't have any data associated with them
4646
at present so we must use the root family node for the task icon.
4747
We don't use this for the v-cylc-object as that would set the node
@@ -56,7 +56,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
5656
</div>
5757
</slot>
5858
<slot name="family-proxy" v-else-if="node.type === 'family'">
59-
<div :class="getNodeDataClass()" @click="nodeClicked">
59+
<div :class="nodeDataClass" @click="nodeClicked">
6060
<Task
6161
v-cylc-object="node"
6262
:key="node.id"
@@ -66,7 +66,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
6666
</div>
6767
</slot>
6868
<slot name="task-proxy" v-else-if="node.type === 'task'">
69-
<div :class="getNodeDataClass()" @click="nodeClicked">
69+
<div :class="nodeDataClass" @click="nodeClicked">
7070
<!-- Task summary -->
7171
<Task
7272
v-cylc-object="node"
@@ -89,7 +89,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
8989
</div>
9090
</slot>
9191
<slot name="job" v-else-if="node.type === 'job'">
92-
<div :class="getNodeDataClass()" @click="nodeClicked">
92+
<div :class="nodeDataClass" @click="nodeClicked">
9393
<Job
9494
v-cylc-object="node"
9595
:key="node.id"
@@ -184,7 +184,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
184184
name="node"
185185
v-else
186186
>
187-
<div :class="getNodeDataClass()">
187+
<div :class="nodeDataClass">
188188
<span
189189
v-if="node && node.node"
190190
@click="nodeClicked"
@@ -263,6 +263,9 @@ const passthroughEvents = [
263263
'tree-item-clicked'
264264
]
265265
266+
/** Margin between expand/collapse btn & node content */
267+
const nodeContentPad = 6 // px
268+
266269
export default {
267270
name: 'TreeItem',
268271
@@ -383,10 +386,14 @@ export default {
383386
expanded: this.isExpanded
384387
}
385388
},
389+
nodeDataClass () {
390+
return ['node-data', `node-data-${this.node.type}`]
391+
},
386392
expandCollapseBtnStyle () {
387393
return {
388394
// set visibility 'hidden' to ensure element takes up space
389-
visibility: this.hasChildren ? null : 'hidden'
395+
visibility: this.hasChildren ? null : 'hidden',
396+
marginRight: `${nodeContentPad}px`,
390397
}
391398
},
392399
/**
@@ -402,7 +409,7 @@ export default {
402409
/** Make the job details triangle point to the job icon */
403410
leafTriangleStyle () {
404411
return {
405-
'margin-left': `${this.nodeIndentation}px`
412+
'margin-left': `${this.nodeIndentation + nodeContentPad}px`
406413
}
407414
},
408415
jobMessageOutputs () {
@@ -453,12 +460,6 @@ export default {
453460
nodeClicked (e) {
454461
this.$emit('tree-item-clicked', this)
455462
},
456-
getNodeDataClass () {
457-
const classes = {}
458-
classes['node-data'] = true
459-
classes[`node-data-${this.node.type}`] = true
460-
return classes
461-
},
462463
latestJob
463464
}
464465
}

src/styles/cylc/_tree.scss

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ $line-height: 1.8em;
3131
*/
3232
$visible-outputs: 5;
3333

34+
/* Task/job icon size. */
35+
$icon-size: 1.2em;
36+
3437
@mixin active-state() {
3538
background-color: $active-color;
3639
&:hover {
@@ -70,7 +73,6 @@ $visible-outputs: 5;
7073
}
7174

7275
.node-data {
73-
margin-left: 6px;
7476
display: flex;
7577
flex-wrap: nowrap;
7678
.node-summary {
@@ -83,14 +85,14 @@ $visible-outputs: 5;
8385
.c-task, .c-job {
8486
display: flex;
8587
align-items: center;
86-
font-size: 1.2em;
88+
font-size: $icon-size;
8789
}
8890
}
8991
.type {
9092
margin-right: 10px;
9193
}
9294

93-
$arrow-size: 15px;
95+
$arrow-size: calc($icon-size / 2);
9496
$leaf-background-color: map-get(settings.$grey, 'lighten-3');
9597

9698
.leaf {

src/utils/font-size.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717

1818
// Module for font size functions and constants.
1919

20-
/** Default <html> font size in px */
21-
const DEFAULT_FONT_SIZE = window.getComputedStyle(document.documentElement).fontSize
22-
export const INCREMENT = 2 // px
20+
/** Font size increment in px */
21+
export const INCREMENT = 2
2322

2423
/**
25-
* Sets the font-size to a value. The default value is the <html> element's
26-
* computed font size.
24+
* Sets the font-size to a value.
2725
*
28-
* @param {string} size in px
26+
* @param {?string} size - Value with units given (doesn't matter which unit).
27+
* If null then reset to default.
2928
*/
30-
export function resetFontSize (size = DEFAULT_FONT_SIZE) {
29+
export function resetFontSize (size = null) {
3130
localStorage.fontSize = size
32-
document.body.style.fontSize = size
31+
document.documentElement.style.fontSize = size
3332
}
3433

3534
export function decreaseFontSize () {
@@ -43,9 +42,9 @@ export function increaseFontSize () {
4342
/**
4443
* Get HTML element (computed) font size.
4544
*
46-
* @returns {number} current font size
45+
* @returns {number} current font size in px
4746
*/
4847
export function getCurrentFontSize () {
49-
const fontSize = localStorage.fontSize ?? window.getComputedStyle(document.body).fontSize
48+
const fontSize = window.getComputedStyle(document.documentElement).fontSize // px
5049
return parseFloat(fontSize)
5150
}

tests/e2e/specs/userprofile.cy.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import {
19-
getCurrentFontSize,
20-
resetFontSize,
21-
INCREMENT
22-
} from '@/utils/font-size'
18+
import { INCREMENT } from '@/utils/font-size'
19+
20+
/** @param {number} expected font size in px */
21+
function expectFontSize (expected) {
22+
cy.get('html')
23+
.should('have.css', 'font-size', `${expected}px`)
24+
}
2325

2426
describe('User Profile', () => {
2527
const defaultFontSize = 16 // px
2628
beforeEach(() => {
27-
resetFontSize()
29+
// resetFontSize()
2830
cy.visit('/#/user-profile')
2931
})
3032

@@ -35,43 +37,38 @@ describe('User Profile', () => {
3537
})
3638

3739
it('Increases the font size', () => {
38-
expect(getCurrentFontSize()).to.equal(defaultFontSize)
40+
expectFontSize(defaultFontSize)
3941
const clicks = 3
40-
cy.get('button#font-size-increase-button').then(($button) => {
41-
for (let i = 0; i < clicks; i++) {
42-
$button.trigger('click')
43-
}
44-
const expectedFontSize = defaultFontSize + INCREMENT * clicks
45-
expect(getCurrentFontSize()).to.equal(expectedFontSize)
46-
})
42+
let expectedFontSize = defaultFontSize
43+
for (let i = 0; i < clicks; i++) {
44+
expectedFontSize += INCREMENT
45+
cy.get('button#font-size-increase-button')
46+
.click()
47+
expectFontSize(expectedFontSize)
48+
}
4749
})
4850

4951
it('Decreases the font size', () => {
50-
expect(getCurrentFontSize()).to.equal(defaultFontSize)
52+
expectFontSize(defaultFontSize)
5153
const clicks = 3
52-
cy.get('button#font-size-decrease-button').then(($button) => {
53-
for (let i = 0; i < clicks; i++) {
54-
$button.trigger('click')
55-
}
56-
const expectedFontSize = defaultFontSize - INCREMENT * clicks
57-
expect(getCurrentFontSize()).to.equal(expectedFontSize)
58-
})
54+
let expectedFontSize = defaultFontSize
55+
for (let i = 0; i < clicks; i++) {
56+
expectedFontSize -= INCREMENT
57+
cy.get('button#font-size-decrease-button')
58+
.click()
59+
expectFontSize(expectedFontSize)
60+
}
5961
})
6062

6163
it('Resets the font size', () => {
62-
expect(getCurrentFontSize()).to.equal(defaultFontSize)
63-
const clicks = 3
64-
cy.get('button#font-size-decrease-button').then(($button) => {
65-
for (let i = 0; i < clicks; i++) {
66-
$button.trigger('click')
67-
}
68-
expect(getCurrentFontSize()).not.to.equal(defaultFontSize)
69-
cy.get('button#font-size-reset-button')
64+
expectFontSize(defaultFontSize)
65+
for (let i = 0; i < 3; i++) {
66+
cy.get('button#font-size-decrease-button')
7067
.click()
71-
.then(() => {
72-
expect(getCurrentFontSize()).to.equal(defaultFontSize)
73-
})
74-
})
68+
}
69+
cy.get('button#font-size-reset-button')
70+
.click()
71+
expectFontSize(defaultFontSize)
7572
})
7673

7774
it('Sets the job theme', () => {

tests/unit/utils/font-size.spec.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,22 @@ describe('Font Size', () => {
2525

2626
beforeEach(() => {
2727
delete localStorage.fontSize
28-
document.body.style.fontSize = `${initialFontSize}px`
28+
document.documentElement.style.fontSize = `${initialFontSize}px`
2929
})
3030

3131
it('gets the current font size', () => {
3232
expect(getCurrentFontSize()).to.equal(initialFontSize)
33-
const newVal = 27
34-
// localStorage value should take precedence
35-
localStorage.fontSize = `${newVal}px`
36-
expect(getCurrentFontSize()).to.equal(newVal)
3733
})
3834

3935
it('sets and gets a new font size', () => {
4036
const newVal = 31
4137
const newValPx = `${newVal}px`
4238
expect(localStorage.fontSize).to.not.equal(newValPx)
43-
expect(document.body.style.fontSize).to.not.equal(newValPx)
39+
expect(document.documentElement.style.fontSize).to.not.equal(newValPx)
4440
expect(getCurrentFontSize()).to.not.equal(newVal)
4541
resetFontSize(newValPx)
4642
expect(localStorage.fontSize).to.equal(newValPx)
47-
expect(document.body.style.fontSize).to.equal(newValPx)
43+
expect(document.documentElement.style.fontSize).to.equal(newValPx)
4844
expect(getCurrentFontSize()).to.equal(newVal)
4945
})
5046

0 commit comments

Comments
 (0)