Merge mounter and view

This commit is contained in:
Misode
2020-12-05 16:38:07 +01:00
parent 53c2973c34
commit 59c143156f
5 changed files with 26 additions and 91 deletions

View File

@@ -1,6 +1,6 @@
import { Hook, ModelPath, Path, StringHookParams, ValidationOption, EnumOption, INode, DataModel, MapNode, StringNode } from '@mcschema/core'
import { locale, segmentedLocale } from '../Locales'
import { Mounter } from '../Mounter'
import { Mounter } from '../views/View'
import { hexId, htmlEncode } from '../Utils'
import { suffixInjector } from './suffixInjector'
@@ -22,10 +22,10 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
},
boolean({ node }, path, value, mounter) {
const onFalse = mounter.registerClick(el => {
const onFalse = mounter.onClick(el => {
path.model.set(path, node.optional() && value === false ? undefined : false)
})
const onTrue = mounter.registerClick(el => {
const onTrue = mounter.onClick(el => {
path.model.set(path, node.optional() && value === true ? undefined : true)
})
return ['', `<button${value === false ? ' class="selected"' : ' '}
@@ -61,11 +61,11 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
},
list({ children }, path, value, mounter) {
const onAdd = mounter.registerClick(el => {
const onAdd = mounter.onClick(el => {
if (!Array.isArray(value)) value = []
path.model.set(path, [children.default(), ...value])
})
const onAddBottom = mounter.registerClick(el => {
const onAddBottom = mounter.onClick(el => {
if (!Array.isArray(value)) value = []
path.model.set(path, [...value, children.default()])
})
@@ -74,7 +74,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
let body = ''
if (Array.isArray(value)) {
body = value.map((childValue, index) => {
const removeId = mounter.registerClick(el => path.model.set(path.push(index), undefined))
const removeId = mounter.onClick(el => path.model.set(path.push(index), undefined))
const childPath = path.push(index).contextPush('entry')
const category = children.category(childPath)
const [cPrefix, cSuffix, cBody] = children.hook(this, childPath, childValue, mounter)
@@ -101,7 +101,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
map({ keys, children }, path, value, mounter) {
const keyPath = new ModelPath(keysModel, new Path([hashString(path.toString())]))
const onAdd = mounter.registerClick(el => {
const onAdd = mounter.onClick(el => {
const key = keyPath.get()
path.model.set(path.push(key), children.default())
})
@@ -111,7 +111,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
if (typeof value === 'object' && value !== undefined) {
body = Object.keys(value)
.map(key => {
const removeId = mounter.registerClick(el => path.model.set(path.push(key), undefined))
const removeId = mounter.onClick(el => path.model.set(path.push(key), undefined))
const childPath = path.modelPush(key)
const category = children.category(childPath)
const [cPrefix, cSuffix, cBody] = children.hook(this, childPath, value[key], mounter)
@@ -131,7 +131,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
},
number({ integer, config }, path, value, mounter) {
const onChange = mounter.registerChange(el => {
const onChange = mounter.onChange(el => {
const value = (el as HTMLInputElement).value
let parsed = config?.color
? parseInt(value.slice(1), 16)
@@ -149,9 +149,9 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
let prefix = ''
if (node.optional()) {
if (value === undefined) {
prefix = `<button class="collapse closed" data-id="${mounter.registerClick(() => path.model.set(path, node.default()))}"></button>`
prefix = `<button class="collapse closed" data-id="${mounter.onClick(() => path.model.set(path, node.default()))}"></button>`
} else {
prefix = `<button class="collapse open" data-id="${mounter.registerClick(() => path.model.set(path, undefined))}"></button>`
prefix = `<button class="collapse open" data-id="${mounter.onClick(() => path.model.set(path, undefined))}"></button>`
}
}
let suffix = node.hook(suffixInjector, path, mounter) || ''

View File

@@ -2,7 +2,7 @@ import { Hook, ModelPath, Path } from '@mcschema/core'
import { App, Previews } from '../App'
import { Octicon } from '../components/Octicon'
import { locale } from '../Locales'
import { Mounter } from '../Mounter'
import { Mounter } from '../views/View'
import { BiomeNoisePreview } from '../preview/BiomeNoisePreview'
import { Preview } from '../preview/Preview'
import { Tracker } from '../Tracker'
@@ -32,7 +32,7 @@ export const suffixInjector: Hook<[Mounter], string | void> = {
&& path.pop().pop().endsWith(new Path(['generator', 'biome_source', 'biomes']))) {
const biomePreview = Previews.biome_noise as BiomeNoisePreview
const biome = path.get()
const id = mounter.registerChange(el => {
const id = mounter.onChange(el => {
biomePreview.setBiomeColor(biome, (el as HTMLInputElement).value)
})
return `<input type="color" value="${biomePreview.getBiomeHex(biome)}" data-id=${id}></input>`
@@ -41,7 +41,7 @@ export const suffixInjector: Hook<[Mounter], string | void> = {
}
function setPreview(preview: Preview, path: ModelPath, mounter: Mounter) {
const id = mounter.registerClick(() => {
const id = mounter.onClick(() => {
Tracker.setPreview(preview.getName())
preview.path = path
App.preview.set(preview)