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

Fix dropdown button is not rendered with selected font. #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ <h2>Download</h2>
"Open Sans", // Default font
{ limit: 30 }, // Additional options
);
setTimeout( function () {
fontPicker.addFont('Droid', 0, 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/default/droidsansmono.ttf')
}, 500)
</script>
</body>
</html>
7 changes: 4 additions & 3 deletions src/FontPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class FontPicker {
li.appendChild(fontButton);

// Insert font button at the specified index. If not specified, append to the end of the list
if (listIndex) {
if (listIndex !== undefined && listIndex !== null) {
this.ul.insertBefore(li, this.ul.children[listIndex]);
} else {
this.ul.appendChild(li);
Expand Down Expand Up @@ -226,15 +226,15 @@ export default class FontPicker {
/**
* Add font to font picker and font map
*/
public addFont(fontFamily: string, index?: number): void {
public addFont(fontFamily: string, index?: number, url = ''): void {
if (Array.from(this.fontManager.getFonts().keys()).includes(fontFamily)) {
throw Error(
`Did not add font to font picker: Font family "${fontFamily}" is already in the list`,
);
}

// Add font to font map in FontManager
this.fontManager.addFont(fontFamily, true);
this.fontManager.addFont(fontFamily, true, url);

// Add font to list in font picker
const font = this.fontManager.getFonts().get(fontFamily);
Expand Down Expand Up @@ -286,6 +286,7 @@ export default class FontPicker {

// Write new font family in dropdown button and highlight font entry in the list
this.dropdownFamily.textContent = fontFamily;
this.dropdownFamily.style.setProperty("font-family", fontFamily);
if (this.activeFontButton) {
this.activeFontButton.classList.remove("active-font");
this.activeFontButton = document.getElementById(
Expand Down