Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffroy-noel-ddh committed Oct 16, 2024
2 parents 671a13f + be6e0b3 commit ccccad5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 20 deletions.
99 changes: 82 additions & 17 deletions app/assets/definitions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ const allographUri = '/digipal/api/allograph/?@select=*script_set,*allograph_com
const collectionUri = './data/dts/api/collections-2023-01.json'
const definitionsPath = 'app/data/pal/definitions-digipal.json'
const STATS_PATH = 'app/stats.json'
const SHA_UNREAD = 'SHA_UNREAD'
const VARIANT_RULES_PATH = 'app/data/variant-rules.json'

createApp({
data() {
return {
archetypeUri: '//digipal.eu',

definitions: {'k1': 'v1'},
definitionsSha: null,
definitionsSha: SHA_UNREAD,
areDefinitionsUnsaved: 0,

//
variantRules: [],
variantRulesSha: SHA_UNREAD,
areVariantRulesUnsaved: 0,

selection: {
script: '',
scriptName: '',
Expand All @@ -27,18 +37,21 @@ createApp({
component: '',
feature: '',
},
isUnsaved: 0,
messages: [],
afs: null,
stats: {},
}
},
computed: {
tabs: () => utils.tabs(),
isUnsaved() {
return this.areDefinitionsUnsaved || this.areVariantRulesUnsaved
},
innerTabs() {
return [
{title: 'Allographs x Components', key: 'ac'},
{title: 'Components x Features', key: 'cf'},
{title: 'Variant types', key: 'vt'},
]
},
isLoggedIn() {
Expand All @@ -56,6 +69,9 @@ createApp({
filteredFeatures() {
return this.getFilteredDefinitions('features', (a) => a)
},
filteredVariantRules() {
return this.variantRules.filter(r => this.definitions.allographs[`${r.allograph}-${this.selection.script}`])
},
lastMessage() {
let ret = {
content: '',
Expand All @@ -72,6 +88,8 @@ createApp({
await this.initAnyFileSystem()
await this.loadDefinitions()
await this.loadStats()
await this.loadVariantRules()
this.setSelectionFromAddressBar()
},
watch: {
'selection.script'() {
Expand Down Expand Up @@ -119,7 +137,7 @@ createApp({
//
if (name) {
this.definitions.features[featureSlug] = name
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
} else {
// undo change
event.target.textContent = this.definitions.features[featureSlug]
Expand All @@ -134,7 +152,7 @@ createApp({
//
if (name) {
component.name = name
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
} else {
// undo change
event.target.textContent = component.name
Expand All @@ -160,21 +178,21 @@ createApp({
},
onRemoveAllograph(allographSlug) {
delete this.definitions.allographs[allographSlug]
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
onRemoveComponent(componentSlug) {
delete this.definitions.components[componentSlug]
for (let allograph of Object.values(this.definitions.allographs)) {
allograph.components = allograph.components.filter(slug => slug != componentSlug)
}
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
onRemoveFeature(featureSlug) {
delete this.definitions.features[featureSlug]
for (let component of Object.values(this.definitions.components)) {
component.features = component.features.filter(slug => slug != featureSlug)
}
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
removeUndefinedComponentsAndFeatures() {
for (let allograph of Object.values(this.definitions.allographs)) {
Expand Down Expand Up @@ -237,7 +255,7 @@ createApp({
if (item) {
items[slug] = item
this.newItems[itemType] = ''
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
}
},
onCreateScript() {
Expand All @@ -249,22 +267,22 @@ createApp({

this.definitions.scripts[slug] = name
this.selection.script = slug
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
onRenameScript() {
let name = this.selection.scriptName.trim()
if (!name) return;

if (this._itemExist('script', name)) return;
this.definitions.scripts[this.selection.script] = name
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
onDeleteScript() {
// TODO: error management
delete this.definitions.scripts[this.selection.script]
this.selectFirstScript()
// TODO: delete allographs?
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
selectFirstScript() {
this.selection.script = Object.keys(this.definitions.scripts)[0]
Expand All @@ -279,7 +297,7 @@ createApp({
} else {
allo.components.push(componentSlug)
}
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
onClickComponentFeature(component, featureSlug) {
if (!this.canEdit) return;
Expand All @@ -288,7 +306,7 @@ createApp({
} else {
component.features.push(featureSlug)
}
this.isUnsaved = 1
this.areDefinitionsUnsaved = 1
},
async loadStats() {
this.stats = null
Expand Down Expand Up @@ -317,10 +335,50 @@ createApp({
this.logMessage(`Could not load definitions (${res.description})`, 'danger')
}
this.setAddressBarFromSelection()
this.isUnsaved = 0
this.areDefinitionsUnsaved = 0
},
async loadVariantRules() {
let res = await this.afs.readJson(VARIANT_RULES_PATH)
if (res && res.ok) {
this.variantRules = res.data
this.variantRulesSha = res.sha
// sort the rules
this.variantRules = utils.sortMulti(this.variantRules, [
'allograph',
'variant-name'
])
} else {
this.variantRules = []
this.logMessage(`Failed to load variant rules from github (${res.description})`, 'error')
}
},
onRemoveRule(rule) {
this.variantRules = this.variantRules.filter(r => r !== rule)
this.areVariantRulesUnsaved = 1
},
async saveAll() {
// TODO: if definition fails & rules succeed user won't see error.
await this.saveDefinitions()
await this.saveVariantRules()
},
async saveDefinitions() {
if (!this.areDefinitionsUnsaved) return;
this.definitions.updated = new Date().toISOString()
let res = await this.saveToJson(definitionsPath, this.definitions, this.definitionsSha)
if (res?.ok) {
this.definitionsSha = res.sha
this.areDefinitionsUnsaved = 0
}
},
async saveVariantRules() {
if (!this.areVariantRulesUnsaved) return;
let res = await this.saveToJson(VARIANT_RULES_PATH, this.variantRules, this.variantRulesSha)
if (res?.ok) {
this.variantRulesSha = res.sha
this.areVariantRulesUnsaved = 0
}
},
async saveToJson(targetPath, objectToSave, githubSha) {
// let res = await utils.updateGithubJsonFile(definitionsPath, this.definitions, this.getOctokit(), this.definitionsSha)
let res = null
if (DEBUG_DONT_SAVE) {
Expand All @@ -329,16 +387,17 @@ createApp({
description: 'Not saving in DEBUG mode. DEBUG_DONT_SAVE = true.',
}
} else {
res = await this.afs.writeJson(definitionsPath, this.definitions, this.definitionsSha)
res = await this.afs.writeJson(targetPath, objectToSave, githubSha)
}
if (res.ok) {
this.definitionsSha = res.sha
this.isUnsaved = 0
// this.definitionsSha = res.sha
// this.isUnsaved = 0
this.clearMessages()
this.logMessage('Definitions saved.', 'info')
} else {
this.logMessage(`Could not save definitions (${res.description})`, 'danger')
}
return res
},
onShortenCollection(e) {
const self = this;
Expand Down Expand Up @@ -431,11 +490,17 @@ createApp({
return utils.getQueryString()
},
setAddressBarFromSelection() {
// TODO: this is doing nothing?
let searchParams = new URLSearchParams(window.location.search);

let qs = `?${searchParams.toString()}`
decodeURIComponent(qs)
},
setSelectionFromAddressBar() {
let searchParams = new URLSearchParams(window.location.search);

this.selection.innerTab = searchParams.get('itb') || 'ac'
},
logMessage(content, level = 'info') {
// level: info|primary|success|warning|danger
this.messages.push({
Expand Down
2 changes: 1 addition & 1 deletion app/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ th .input {
width: 4em;
}

.btn-remove {
th .btn-remove {
visibility: hidden;
float: right;
}
Expand Down
1 change: 1 addition & 0 deletions app/assets/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ createApp({
// the github sha of the annotations file.
// needed for writing it and detecting conflicts.
changeQueueSha: SHA_UNREAD,
// ---
variantRules: [],
variantRulesSha: SHA_UNREAD,
// ---
Expand Down
47 changes: 46 additions & 1 deletion app/definitions.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<template v-if="!isLoggedIn && tab.key == 'settings'"><i class="fas fa-exclamation-triangle"></i>&nbsp;</template>
{{ tab.title }}
&nbsp;
<button v-if="selection.tab == tab.key" @click.prevent.stop="saveDefinitions()" class="button is-primary" :disabled="!canEdit || !isUnsaved">Save</button>
<button v-if="selection.tab == tab.key" @click.prevent.stop="saveAll()" class="button is-primary" :disabled="!canEdit || !isUnsaved">Save</button>
</a>
</li>
</ul>
Expand Down Expand Up @@ -160,6 +160,51 @@
</table>
</div>

<div v-if="selection.innerTab == 'vt'" class="scrollable-table-wrapper">
<div class="field is-grouped is-horizontal definitions-controls">
<p class="control">
<label class="label">Script</label>
<div class="select">
<select v-model="selection.script">
<option v-for="(scriptName, scriptSlug) in definitions.scripts" :value="scriptSlug">{{ scriptName }}</option>
</select>
</div>
</p>
</div>
<table class="table table-def table-ac is-striped is-narrow is-hoverable is-fullwidth-no is-bordered">
<thead>
<tr>
<th>Character</th>
<th>Component</th>
<th>Feature main</th>
<th>Feature secondary</th>
<th>Type</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="rule in filteredVariantRules">
<td>{{ rule.allograph }}</td>
<td>{{ rule['component-features'][0]['component'] }}</td>
<td>{{ rule['component-features'][0]['feature'] }}</td>
<td>
<template v-if="rule['component-features'][1]">
<template v-if="rule['component-features'][1]['component'] != rule['component-features'][0]['component']">{{ rule['component-features'][1]['component'] }} = <br></template>
{{ rule['component-features'][1]['feature'] }}
</template>
</td>
<td>{{ rule['variant-name'] }}</td>
<td>
&nbsp;
<button v-if="canEdit" @click="onRemoveRule(rule)" class="button is-small btn-remove is-warning" title="remove">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>

<template v-if="false">
<hr>

Expand Down
2 changes: 1 addition & 1 deletion app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h2 class="title is-3">Filters
</div>
</template>
<p class="panel-heading">
New variant type
New <a href="../definitions.html?itb=vt">variant type</a>
</p>
<div class="panel-block type">
<div>
Expand Down
18 changes: 18 additions & 0 deletions app/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ async function mod(exports) {
fs.writeFileSync(path, content)
}

exports.sortMulti = function(arr, fields) {
return arr.sort((a, b) => {
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
if (a[field] < b[field]) return -1;
if (a[field] > b[field]) return 1;
}
return 0; // equal
});
}

exports.getScriptFromAllograph = function(allograph, definitions) {
// TODO
let ret = null

return ret ? ret.script : '?'
}

// --------------------------------------------

if (IS_BROWSER) {
Expand Down

0 comments on commit ccccad5

Please sign in to comment.