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: macro buttons with single char attribute #903

Merged
merged 7 commits into from
Jun 15, 2022
Merged
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
9 changes: 6 additions & 3 deletions src/components/inputs/MacroButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export default class MacroButton extends Mixins(BaseMixin) {
return this.$store.getters['printer/getMacro'](this.macro.name)
}

get isGcodeStyle() {
return this.macro.name.match(/[G|M]\d{1,3}/gm)
}

@Watch('klipperMacro')
klipperMacroChange() {
this.refreshParams()
Expand All @@ -103,7 +107,7 @@ export default class MacroButton extends Mixins(BaseMixin) {
this.paramArray.splice(0, this.paramArray.length)
this.params = {}

if (this.klipperMacro.params !== null) {
if (this.klipperMacro?.params !== null) {
Object.keys(this.klipperMacro.params).forEach((name: string) => {
if (!name.startsWith('_')) {
this.paramArray.push(name)
Expand All @@ -130,8 +134,7 @@ export default class MacroButton extends Mixins(BaseMixin) {
this.paramArray.forEach((paramname: string) => {
if (this.params[paramname].value !== null && this.params[paramname].value !== '') {
let tmp: string = paramname
if (paramname.length === 1) tmp += this.params[paramname].value
else tmp += '=' + this.params[paramname].value
tmp += this.isGcodeStyle ? this.params[paramname].value : `=${this.params[paramname].value}`

params.push(tmp)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/panels/MacrogroupPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class MacrogroupPanel extends Mixins(BaseMixin) {
}

get allMacros() {
return this.$store.getters['printer/getAllMacros'] ?? []
return this.$store.getters['printer/getMacros'] ?? []
}

get macros() {
Expand Down
9 changes: 8 additions & 1 deletion src/components/panels/MacrosPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ import BaseMixin from '../mixins/base'
import Panel from '@/components/ui/Panel.vue'
import MacroButton from '@/components/inputs/MacroButton.vue'
import { mdiCodeTags } from '@mdi/js'
import { PrinterStateMacro } from '@/store/printer/types'
@Component({
components: { MacroButton, Panel },
})
export default class MacrosPanel extends Mixins(BaseMixin) {
mdiCodeTags = mdiCodeTags

get hiddenMacros() {
return (this.$store.state.gui?.macros?.hiddenMacros ?? []).map((name: string) => name.toLowerCase())
}

get macros() {
return this.$store.getters['printer/getMacros']
const macros = this.$store.getters['printer/getMacros']

return macros.filter((macro: PrinterStateMacro) => !this.hiddenMacros.includes(macro.name.toLowerCase()))
}
}
</script>
2 changes: 1 addition & 1 deletion src/components/settings/SettingsMacrosTabExpert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export default class SettingsMacrosTabExpert extends Mixins(BaseMixin) {
}

get allMacros() {
return this.$store.getters['printer/getAllMacros'] ?? []
return this.$store.getters['printer/getMacros'] ?? []
}

get availableMacros() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings/SettingsMacrosTabSimple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import SettingsRow from '@/components/settings/SettingsRow.vue'
})
export default class SettingsMacrosTabSimple extends Mixins(BaseMixin) {
get macros() {
return this.$store.getters['printer/getAllMacros'] ?? []
return this.$store.getters['printer/getMacros'] ?? []
}

get hiddenMacros() {
Expand Down
89 changes: 26 additions & 63 deletions src/store/printer/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,40 @@ export const getters: GetterTree<PrinterState, RootState> = {
return state.virtual_sdcard?.progress ?? 0
},

getMacros: (state, getters, rootState) => {
getMacros: (state) => {
const array: PrinterStateMacro[] = []
const hiddenMacros: string[] = []
const config = state.configfile?.config ?? {}
const settings = state.configfile?.settings ?? null

rootState.gui?.macros?.hiddenMacros.forEach((item: string, index: number) => {
hiddenMacros[index] = item.toLowerCase()
})
Object.keys(config)
.filter((prop) => prop.toLowerCase().startsWith('gcode_macro'))
.forEach((prop) => {
const name = prop.replace('gcode_macro ', '')
if (name.startsWith('_')) return

if (state.configfile?.config) {
Object.keys(state.configfile?.config).forEach((prop) => {
if (
prop.startsWith('gcode_macro') &&
!prop.startsWith('gcode_macro _') &&
!('rename_existing' in state.configfile.config[prop]) &&
!(hiddenMacros.indexOf(prop.replace('gcode_macro ', '').toLowerCase()) > -1)
) {
const variables = state[prop] ?? {}

array.push({
name: prop.replace('gcode_macro ', ''),
description: state.configfile.config[prop].description ?? null,
prop: state.configfile.config[prop],
params: getMacroParams(state.configfile.config[prop]),
variables,
})
}
const propLower = prop.toLowerCase()
const propSettings = settings[propLower]
if ('rename_existing' in propSettings) return

const variables = state[prop] ?? {}

array.push({
name,
description: settings[propLower].description ?? null,
prop: propSettings,
params: getMacroParams(propSettings),
variables,
})
})
}

return caseInsensitiveSort(array, 'name')
},

getMacro: (state, getters) => (name: string) => {
const nameLower = name.toLowerCase()
return getters['getMacros'].find((macro: PrinterStateMacro) => macro.name.toLowerCase() === nameLower)
},

getHeaters: (state, getters, rootState, rootGetters) => {
const heaters: PrinterStateHeater[] = []
const colorOff = 'grey darken-2'
Expand Down Expand Up @@ -461,45 +463,6 @@ export const getters: GetterTree<PrinterState, RootState> = {
return state.heaters?.available_sensors ?? []
},

getAllMacros: (state) => {
const array: PrinterStateMacro[] = []

Object.keys(state.configfile?.config ?? {}).forEach((prop) => {
if (
prop.startsWith('gcode_macro') &&
!prop.startsWith('gcode_macro _') &&
!Object.hasOwnProperty.call(state.configfile.config[prop], 'rename_existing')
) {
const variables = state[prop] ?? {}

array.push({
name: prop.replace('gcode_macro ', ''),
description: state.configfile.config[prop].description ?? null,
prop: state.configfile.config[prop],
params: getMacroParams(state.configfile.config[prop]),
variables,
})
}
})

return caseInsensitiveSort(array, 'name')
},

getMacro: (state) => (name: string) => {
if ('gcode_macro ' + name in state.configfile.config) {
const config = state.configfile.config['gcode_macro ' + name]

return {
name,
description: config.description ?? null,
prop: config,
params: getMacroParams(config),
}
}

return null
},

getFilamentSensors: (state) => {
const sensorObjectNames = ['filament_switch_sensor', 'filament_motion_sensor']
const sensors: PrinterStateFilamentSensors[] = []
Expand Down