added validation
This commit is contained in:
parent
098879be3e
commit
973e8697bc
|
@ -16,6 +16,7 @@ import {
|
||||||
getCssRules,
|
getCssRules,
|
||||||
getScopedVersion
|
getScopedVersion
|
||||||
} from 'src/services/theme_data/css_utils.js'
|
} from 'src/services/theme_data/css_utils.js'
|
||||||
|
import { deserialize } from 'src/services/theme_data/iss_deserializer.js'
|
||||||
|
|
||||||
import SharedComputedObject from '../helpers/shared_computed_object.js'
|
import SharedComputedObject from '../helpers/shared_computed_object.js'
|
||||||
import ProfileSettingIndicator from '../helpers/profile_setting_indicator.vue'
|
import ProfileSettingIndicator from '../helpers/profile_setting_indicator.vue'
|
||||||
|
@ -39,6 +40,7 @@ const AppearanceTab = {
|
||||||
accept: '.json, .piss',
|
accept: '.json, .piss',
|
||||||
validator: this.importValidator,
|
validator: this.importValidator,
|
||||||
onImport: this.onImport,
|
onImport: this.onImport,
|
||||||
|
parser: this.importParser,
|
||||||
onImportFailure: this.onImportFailure
|
onImportFailure: this.onImportFailure
|
||||||
}),
|
}),
|
||||||
palettesKeys: [
|
palettesKeys: [
|
||||||
|
@ -263,21 +265,33 @@ const AppearanceTab = {
|
||||||
importFile () {
|
importFile () {
|
||||||
this.fileImporter.importData()
|
this.fileImporter.importData()
|
||||||
},
|
},
|
||||||
|
importParser (file, filename) {
|
||||||
|
if (filename.endsWith('.json')) {
|
||||||
|
return JSON.parse(file)
|
||||||
|
} else if (filename.endsWith('.piss')) {
|
||||||
|
return deserialize(file)
|
||||||
|
}
|
||||||
|
},
|
||||||
onImport (parsed, filename) {
|
onImport (parsed, filename) {
|
||||||
if (filename.endsWith('.json')) {
|
if (filename.endsWith('.json')) {
|
||||||
this.$store.dispatch('setThemeCustom', parsed.source || parsed.theme)
|
this.$store.dispatch('setThemeCustom', parsed.source || parsed.theme)
|
||||||
this.$store.dispatch('applyTheme')
|
} else if (filename.endsWith('.piss')) {
|
||||||
|
this.$store.dispatch('setStyleCustom', parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this.loadTheme(parsed, 'file', forceSource)
|
|
||||||
},
|
},
|
||||||
onImportFailure (result) {
|
onImportFailure (result) {
|
||||||
|
console.error('Failure importing theme:', result)
|
||||||
this.$store.dispatch('pushGlobalNotice', { messageKey: 'settings.invalid_theme_imported', level: 'error' })
|
this.$store.dispatch('pushGlobalNotice', { messageKey: 'settings.invalid_theme_imported', level: 'error' })
|
||||||
},
|
},
|
||||||
importValidator (parsed, filename) {
|
importValidator (parsed, filename) {
|
||||||
if (filename.endsWith('.json')) {
|
if (filename.endsWith('.json')) {
|
||||||
const version = parsed._pleroma_theme_version
|
const version = parsed._pleroma_theme_version
|
||||||
return version >= 1 || version <= 2
|
return version >= 1 || version <= 2
|
||||||
|
} else if (filename.endsWith('.piss')) {
|
||||||
|
if (!Array.isArray(parsed)) return false
|
||||||
|
if (parsed.length < 1) return false
|
||||||
|
if (parsed.find(x => x.component === '@meta') == null) return false
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isThemeActive (key) {
|
isThemeActive (key) {
|
||||||
|
|
|
@ -597,7 +597,11 @@ export default {
|
||||||
|
|
||||||
const styleImporter = newImporter({
|
const styleImporter = newImporter({
|
||||||
accept: '.piss',
|
accept: '.piss',
|
||||||
parser: (string) => deserialize(string),
|
parser (string) { return deserialize(string) },
|
||||||
|
onImportFailure (result) {
|
||||||
|
console.error('Failure importing style:', result)
|
||||||
|
this.$store.dispatch('pushGlobalNotice', { messageKey: 'settings.invalid_theme_imported', level: 'error' })
|
||||||
|
},
|
||||||
onImport (parsed, filename) {
|
onImport (parsed, filename) {
|
||||||
const editorComponents = parsed.filter(x => x.component.startsWith('@'))
|
const editorComponents = parsed.filter(x => x.component.startsWith('@'))
|
||||||
const rootComponent = parsed.find(x => x.component === 'Root')
|
const rootComponent = parsed.find(x => x.component === 'Root')
|
||||||
|
|
|
@ -46,7 +46,7 @@ export const newImporter = ({
|
||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
reader.onload = ({ target }) => {
|
reader.onload = ({ target }) => {
|
||||||
try {
|
try {
|
||||||
const parsed = parser(target.result)
|
const parsed = parser(target.result, filename)
|
||||||
const validationResult = validator(parsed, filename)
|
const validationResult = validator(parsed, filename)
|
||||||
if (validationResult === true) {
|
if (validationResult === true) {
|
||||||
onImport(parsed, filename)
|
onImport(parsed, filename)
|
||||||
|
|
Loading…
Reference in a new issue