Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/no angular rendering #29

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-pianos-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openproject/octicons': patch
---

feat: change icon names & selectors
5 changes: 5 additions & 0 deletions .changeset/tidy-cycles-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openproject/octicons': patch
---

feat: add rendering function outside of Angular
5 changes: 3 additions & 2 deletions lib/octicons_angular/script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const icons = Object.entries(octicons)
export const ${svgDataExportName} = ${generate(svgData).code};

@Component({
selector: 'svg[op-octicon-${key}]',
selector: 'svg[${key}-icon]',
standalone: true,
imports: [NgIf, NgFor],
template: \`
Expand Down Expand Up @@ -81,7 +81,8 @@ async function writeIconExport(file) {
const code = `${GENERATED_HEADER}
import { Component } from '@angular/core';
import { NgFor, NgIf } from '@angular/common';
import { OpOcticonComponentBase, SVGData } from '../octicon-component-base';
import { OpOcticonComponentBase } from '../octicon-component-base';
import { SVGData } from '../helpers';

${icons.map(({code}) => code).join('\n')}
`
Expand Down
29 changes: 29 additions & 0 deletions lib/octicons_angular/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
export type SVGSize = 'small'|'medium'|'large';

export interface SVGData {
[key: string]: {
width: number,
paths: string[],
};
};

export const sizeMap = {
small: 16,
medium: 32,
large: 64
};

export function closestNaturalHeight(naturalHeights:string[], height:number) {
return naturalHeights
.map(naturalHeight => parseInt(naturalHeight, 10))
.reduce((acc, naturalHeight) => (naturalHeight <= height ? naturalHeight : acc), parseInt(naturalHeights[0], 10))
}

export function toDOMString(data:SVGData, size:SVGSize = 'medium', extraAttributes:Record<string, string> = {}) {
const height = sizeMap[size];
const naturalHeight = closestNaturalHeight(Object.keys(data), height)
const { width, paths } = data[naturalHeight.toString()];
const elWidth = height * (width / naturalHeight);
return `<svg
height="${height}px"
width="${elWidth}px"
${Object.keys(extraAttributes).reduce((total, attr) => `${total} ${attr}="${extraAttributes[attr]}"`, '')}
>
${paths.map(p => `<path d="${p}"></path>`)}
</svg>`
}
16 changes: 2 additions & 14 deletions lib/octicons_angular/src/octicon-component-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ import {
HostBinding
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { closestNaturalHeight } from './helpers';

export interface SVGData {
[key: string]: {
width: number,
paths: string[],
};
};
import { closestNaturalHeight, SVGData, SVGSize, sizeMap } from './helpers';

@Directive({})
export class OpOcticonComponentBase {
@Input() className = '';
@Input() fill = 'currentColor';
@Input() size: 'small'|'medium'|'large'= 'medium';
@Input() size:SVGSize = 'medium';
@Input() verticalAlign = 'text-bottom';
@Input() title = '';

Expand Down Expand Up @@ -52,11 +45,6 @@ export class OpOcticonComponentBase {

@HostBinding('attr.height')
get height() {
const sizeMap = {
small: 16,
medium: 32,
large: 64
}
return sizeMap[this.size];
}

Expand Down
42 changes: 34 additions & 8 deletions lib/octicons_angular/src/public-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
opAddIconData,
LogIconComponent,
logIconData,

toDOMString,
} from './public-api';

describe('Github native icon', () => {
Expand All @@ -28,19 +30,19 @@ describe('Github native icon', () => {

it('should render the svg', () => {
const iconElement: HTMLElement = fixture.nativeElement;
expect(iconElement.children[0].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[0].tagName.toLowerCase()).toEqual('path');
expect(iconElement.children[0].getAttribute('d')).toBeTruthy();
});

it('should render the title', () => {
const iconElement: HTMLElement = fixture.nativeElement;
expect(iconElement.children[0].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[0].tagName.toLowerCase()).toEqual('path');

component.title = "Some title";
component.title = 'Some title';
fixture.detectChanges();

expect(iconElement.children[0].tagName.toLowerCase()).toEqual("title");
expect(iconElement.children[1].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[0].tagName.toLowerCase()).toEqual('title');
expect(iconElement.children[1].tagName.toLowerCase()).toEqual('path');
expect(iconElement.children[1].getAttribute('d')).toBeTruthy();
});

Expand Down Expand Up @@ -71,7 +73,7 @@ describe('OpenProject extension icon', () => {

it('should render the svg', () => {
const iconElement: HTMLElement = fixture.nativeElement;
expect(iconElement.children[0].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[0].tagName.toLowerCase()).toEqual('path');
expect(iconElement.children[0].getAttribute('d')).toBeTruthy();
});

Expand Down Expand Up @@ -102,9 +104,9 @@ describe('Icon with multiple paths', () => {

it('should render the svg with all paths', () => {
const iconElement: HTMLElement = fixture.nativeElement;
expect(iconElement.children[0].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[0].tagName.toLowerCase()).toEqual('path');
expect(iconElement.children[0].getAttribute('d')).toBeTruthy();
expect(iconElement.children[1].tagName.toLowerCase()).toEqual("path");
expect(iconElement.children[1].tagName.toLowerCase()).toEqual('path');
expect(iconElement.children[1].getAttribute('d')).toBeTruthy();
});

Expand All @@ -115,3 +117,27 @@ describe('Icon with multiple paths', () => {
});
});


describe('rendering without Angular', () => {
it('should render the SVG', () => {
const rendered = toDOMString(logIconData);
expect(rendered).toContain('<svg');
expect(rendered).toContain(`<path d="${logIconData[24].paths[0]}"></path>`);
expect(rendered).toContain('</svg>');
});

it('should render the small SVG', () => {
const rendered = toDOMString(logIconData, 'small');
expect(rendered).toContain('<svg');
expect(rendered).toContain(`<path d="${logIconData[16].paths[0]}"></path>`);
expect(rendered).toContain('</svg>');
});

it('should render the SVG attributes', () => {
const rendered = toDOMString(logIconData, 'medium', { extra: '1' });
expect(rendered).toContain('<svg');
expect(rendered).toContain('extra="1"');
expect(rendered).toContain(`<path d="${logIconData[24].paths[0]}"></path>`);
expect(rendered).toContain('</svg>');
});
});
1 change: 1 addition & 0 deletions lib/octicons_angular/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*/

export * from './__generated__/icons';
export * from './helpers';
Loading