Skip to content

Commit 42bd284

Browse files
authored
Merge pull request #23 from BenCreating/convert-to-web-components
Convert to web components
2 parents 86b6c22 + 8dbdea6 commit 42bd284

16 files changed

Lines changed: 393 additions & 382 deletions

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import './javascript/web-components/lpc-animation-preview.component.js'
2+
import './javascript/web-components/lpc-asset-category.component.js'
3+
import './javascript/web-components/lpc-category-palettes.component.js'
4+
import './javascript/web-components/lpc-option-sidebar.component.js'
5+
import './javascript/web-components/lpc-attribution.component.js'
26
import CharacterGenerator from './javascript/CharacterGenerator.js'
37

48
const characterGenerator = new CharacterGenerator()

index.template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
<div class="editor">
21-
<div class="sidebar"></div>
21+
<lpc-option-sidebar></lpc-option-sidebar>
2222

2323
<div class="spritesheet-container">
2424
<canvas id="spritesheet"></canvas>
@@ -27,7 +27,7 @@
2727

2828
<div class="attribution">
2929
<button type="button" id="copy-attribution-button">Copy</button>
30-
<div class="attribution-content"></div>
30+
<lpc-attribution></lpc-attribution>
3131
</div>
3232
</body>
3333
</html>

javascript/AssetCategory.js

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default class AssetCategory {
3939
get paletteDefinitions() { return this.optionController.paletteDefinitions }
4040
get categoryDefinitions() { return this.optionController.categoryDefinitions }
4141

42+
categoryId() {
43+
return `${this.name}-category`
44+
}
45+
4246
/**
4347
* Returns the tags for the selected option
4448
*
@@ -88,9 +92,7 @@ export default class AssetCategory {
8892

8993
if (redrawSpritesheet) {
9094
this.optionController.fixExcludedOptions(this)
91-
this.optionController.update()
9295
await this.spritesheetController.update()
93-
this.attributionController.update()
9496
this.optionController.updatePreviewFrameSize()
9597
}
9698
}
@@ -156,57 +158,6 @@ export default class AssetCategory {
156158
return true
157159
}
158160

159-
/**
160-
* Returns the HTML to display this category
161-
*
162-
* @returns {HTMLElement}
163-
*/
164-
html() {
165-
const name = this.name
166-
167-
const container = document.createElement('fieldset')
168-
container.id = `${name}-options`
169-
170-
const label = document.createElement('legend')
171-
label.textContent = name
172-
container.appendChild(label)
173-
174-
container.appendChild(this.colorsHTML())
175-
176-
const optionsContainer = document.createElement('div')
177-
optionsContainer.className = 'category-item-options'
178-
container.appendChild(optionsContainer)
179-
180-
const options = this.availableOptions()
181-
options.forEach(option => {
182-
optionsContainer.appendChild(option.html())
183-
})
184-
185-
return container
186-
}
187-
188-
/**
189-
* Returns the HTML for this category's color options
190-
*
191-
* @returns {HTMLElement}
192-
*/
193-
colorsHTML() {
194-
const colorsContatinerID = `${this.name}-colors`
195-
const previousColors = document.getElementById(colorsContatinerID)
196-
if (previousColors) previousColors.remove()
197-
198-
const container = document.createElement('div')
199-
container.id = colorsContatinerID
200-
container.className = 'item-color-options'
201-
202-
this.selectedPaletteNames().forEach(paletteName => {
203-
const palette = this.palettes[paletteName]
204-
container.appendChild(palette.html())
205-
})
206-
207-
return container
208-
}
209-
210161
/**
211162
* Generates all palettes for the options in the category and returns
212163
* key-value list of names and the corresponding palette

javascript/AssetOption.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,8 @@ export default class AssetOption {
3636
return !excludedByTags.find(excludedTag => selectedTags.includes(excludedTag))
3737
}
3838

39-
/**
40-
* The HTML to display this option
41-
*
42-
* @returns {HTMLElement}
43-
*/
44-
html() {
45-
const name = this.name
46-
const buttonId = `option-${this.category.name}-${name}`
47-
48-
const buttonContainer = document.createElement('div')
49-
buttonContainer.className = 'item-button'
50-
51-
const radioButton = document.createElement('input')
52-
radioButton.setAttribute('type', 'radio')
53-
radioButton.setAttribute('name', this.category.name)
54-
radioButton.setAttribute('value', name)
55-
radioButton.id = buttonId
56-
radioButton.addEventListener('click', this.selectOption.bind(this))
57-
buttonContainer.appendChild(radioButton)
58-
59-
radioButton.checked = this === this.category.selectedOption
60-
61-
const label = document.createElement('label')
62-
label.htmlFor = buttonId
63-
buttonContainer.appendChild(label)
64-
65-
if (this.icon) label.appendChild(this.icon)
66-
67-
const labelText = document.createElement('span')
68-
labelText.textContent = this.label ?? name
69-
label.appendChild(labelText)
70-
71-
return buttonContainer
39+
isSelected() {
40+
return this === this.category.selectedOption
7241
}
7342

7443
/**
@@ -80,15 +49,6 @@ export default class AssetOption {
8049
this.category.setSelectedOption(this)
8150
}
8251

83-
/**
84-
* Returns the attribution for this asset as HTML
85-
*
86-
* @returns {HTMLElement}
87-
*/
88-
attributionHTML() {
89-
return this.attribution.html()
90-
}
91-
9252
/**
9353
* Returns the attribution for this asset as plain text
9454
*

javascript/Attribution.js

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@ export default class Attribution {
1616
this.links = assetData.links ?? []
1717
}
1818

19-
/**
20-
* Return the attribution as HTML
21-
*
22-
* @returns {HTMLElement}
23-
*/
24-
html() {
25-
const attribution = document.createElement('li')
26-
const authorAndLicense = document.createElement('span')
27-
28-
authorAndLicense.textContent = this.authorAndLicense()
29-
const linkList = this.linkListHTML()
30-
31-
attribution.appendChild(authorAndLicense)
32-
attribution.appendChild(linkList)
33-
34-
return attribution
35-
}
36-
3719
/**
3820
* Returns the attribution as plain text
3921
*
@@ -57,27 +39,6 @@ export default class Attribution {
5739
return `${this.categoryName}: ${labelAndName} by ${authorList}. Licenses: ${licenseList}.`
5840
}
5941

60-
/**
61-
* Returns the list of links as HTML
62-
*
63-
* @returns {HTMLElement}
64-
*/
65-
linkListHTML() {
66-
const linkList = document.createElement('ul')
67-
68-
this.links.forEach(url => {
69-
const link = document.createElement('a')
70-
link.setAttribute('href', url)
71-
link.textContent = url
72-
73-
const listItem = document.createElement('li')
74-
listItem.appendChild(link)
75-
linkList.appendChild(listItem)
76-
})
77-
78-
return linkList
79-
}
80-
8142
/**
8243
* Returns the list of links as plain text
8344
*

javascript/AttributionController.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
*/
44

55
/**
6-
* Manages and generates the attribution both as HTML (when shown on the page)
7-
* and as plain text (when copied to the clipboard or included in the download)
6+
* Manages and generates the attribution
87
*/
98
export default class AttributionController {
109
/**
@@ -16,18 +15,6 @@ export default class AttributionController {
1615

1716
get optionController() { return this.characterGenerator.optionController }
1817

19-
/**
20-
* Updates the attribution displayed on the page
21-
*/
22-
update() {
23-
const attributionContainer = document.querySelector('.attribution-content')
24-
25-
const authorsHTML = this.authorsHTML()
26-
const attributionHTML = this.attributionHTML()
27-
28-
attributionContainer.replaceChildren(authorsHTML, attributionHTML)
29-
}
30-
3118
/**
3219
* Copies the full plain text attribution to the clipboard
3320
*/
@@ -69,36 +56,6 @@ export default class AttributionController {
6956
return this.authors().join(', ')
7057
}
7158

72-
/**
73-
* Returns a list of all authors who contributed to the spritesheet as HTML
74-
*
75-
* @returns {HTMLElement}
76-
*/
77-
authorsHTML() {
78-
const authorsPlainText = this.authorsPlainText()
79-
80-
const authorsHTML = document.createElement('span')
81-
authorsHTML.textContent = authorsPlainText
82-
83-
return authorsHTML
84-
}
85-
86-
/**
87-
* Returns a detailed attribution for each item in the spritesheet as HTML
88-
*
89-
* @returns {HTMLElement}
90-
*/
91-
attributionHTML() {
92-
const attribution = document.createElement('ul')
93-
94-
this.selectedOptions().forEach(option => {
95-
const optionAttribution = option.attributionHTML()
96-
attribution.appendChild(optionAttribution)
97-
})
98-
99-
return attribution
100-
}
101-
10259
/**
10360
* Returns a detailed attribution for each item in the spritesheet formatted
10461
* as plain text

javascript/CharacterGenerator.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ export default class CharacterGenerator {
3636

3737
await this.optionController.setupOptionButtons()
3838
await this.spritesheetController.update()
39-
this.attributionController.update()
4039

4140
const preview = document.querySelector('lpc-animation-preview')
4241
preview.spritesheetController = this.spritesheetController
4342
preview.start()
43+
44+
const sidebar = document.querySelector('lpc-option-sidebar')
45+
sidebar.optionController = this.optionController
46+
47+
const attribution = document.querySelector('lpc-attribution')
48+
attribution.attributionController = this.attributionController
49+
attribution.selectedOptions = this.attributionController.selectedOptions()
4450
}
4551

4652
/**

javascript/ColorRamp.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,11 @@ export default class ColorRamp {
1414
constructor(palette, name, colors) {
1515
this.palette = palette
1616
this.name = name
17-
this.colors = colors
17+
this.colorList = colors
1818
}
1919

20-
/**
21-
* The HTML for this color option
22-
*
23-
* @returns {HTMLElement}
24-
*/
25-
html() {
26-
const checked = this === this.palette.selectedColorRamp
27-
28-
const radioButton = document.createElement('input')
29-
radioButton.setAttribute('type', 'radio')
30-
radioButton.setAttribute('name', this.palette.urlParameterKey())
31-
radioButton.setAttribute('value', this.name)
32-
radioButton.addEventListener('click', this.selectColor.bind(this))
33-
34-
radioButton.checked = checked
35-
36-
radioButton.className = 'color-option'
37-
radioButton.style.backgroundColor = this.colors[0]
38-
radioButton.style.borderColor = this.colors[1]
39-
40-
return radioButton
20+
get colors() {
21+
return this.colorList
4122
}
4223

4324
/**
@@ -48,4 +29,8 @@ export default class ColorRamp {
4829
selectColor(_event) {
4930
this.palette.setSelectedColorRamp(this)
5031
}
32+
33+
isSelected() {
34+
return this === this.palette.selectedColorRamp
35+
}
5136
}

0 commit comments

Comments
 (0)