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

refactor: refactor Console & MiniConsole #2031

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
138 changes: 138 additions & 0 deletions src/components/inputs/ConsoleTextarea.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<template>
<v-textarea
ref="gcodeCommandField"
v-model="gcode"
:items="items"
:label="$t('Panels.MiniconsolePanel.SendCode')"
solo
class="gcode-command-field"
autocomplete="off"
no-resize
auto-grow
:rows="rows"
hide-details
outlined
dense
:prepend-icon="isTouchDevice ? mdiChevronDoubleRight : ''"
:append-icon="mdiSend"
@keydown.enter.prevent.stop="doSend"
@keyup.up="onKeyUp"
@keyup.down="onKeyDown"
@keydown.tab="onAutocomplete"
@click:prepend="onAutocomplete"
@click:append="doSend" />
</template>
<script lang="ts">
import { Component, Mixins, Ref } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'
import ConsoleMixin from '@/components/mixins/console'
import { mdiSend, mdiChevronDoubleRight } from '@mdi/js'
import { VTextareaType } from '@/store/printer/types'
import { strLongestEqual } from '@/plugins/helpers'

@Component
export default class ConsoleTextarea extends Mixins(BaseMixin, ConsoleMixin) {
mdiSend = mdiSend
mdiChevronDoubleRight = mdiChevronDoubleRight

@Ref() readonly gcodeCommandField!: VTextareaType

gcode = ''
lastCommandNumber: number | null = null
items = []

get rows(): number {
return this.gcode?.split('\n').length ?? 1
}

setGcode(gcode: string): void {
this.gcode = gcode

this.$nextTick(() => {
meteyou marked this conversation as resolved.
Show resolved Hide resolved
this.gcodeCommandField.focus()
})
}

onKeyUp(): void {
if (this.lastCommandNumber === null && this.lastCommands.length) {
this.lastCommandNumber = this.lastCommands.length - 1
this.gcode = this.lastCommands[this.lastCommandNumber]
} else if (this.lastCommandNumber && this.lastCommandNumber > 0) {
this.lastCommandNumber--
this.gcode = this.lastCommands[this.lastCommandNumber]
}
}

onKeyDown(): void {
if (this.lastCommandNumber === null) return

if (this.lastCommandNumber < this.lastCommands.length - 1) {
this.lastCommandNumber++
this.gcode = this.lastCommands[this.lastCommandNumber]
} else if (this.lastCommandNumber === this.lastCommands.length - 1) {
this.lastCommandNumber = null
this.gcode = ''
}
}

doSend(cmd: KeyboardEvent) {
if (cmd.shiftKey) {
this.gcode += '\n'
return
}

if (this.gcode === '') return

this.$store.dispatch('printer/sendGcode', this.gcode)
this.$store.dispatch('gui/gcodehistory/addToHistory', this.gcode)
this.gcode = ''
this.lastCommandNumber = null
}

onAutocomplete(e: Event): void {
meteyou marked this conversation as resolved.
Show resolved Hide resolved
e.preventDefault()

if (!this.gcode.length) return

const textarea = this.gcodeCommandField.$refs.input
const currentPosition = textarea.selectionStart
const beforeCursor = this.gcode.substring(0, currentPosition)
const lastNewlineIndex = beforeCursor.lastIndexOf('\n')
const currentLine = beforeCursor.substring(lastNewlineIndex + 1)

const currentLineLowerCase = currentLine.toLowerCase()
const commands = this.helplist.filter((element) => element.commandLow.startsWith(currentLineLowerCase))

if (commands.length === 0) return

if (commands?.length === 1) {
this.updateGcode(commands[0].command, lastNewlineIndex, currentPosition)
return
}

const longestCommon = commands.reduce((acc, val) => {
return strLongestEqual(acc, val.command)
}, commands[0].command)

let output = ''
commands.forEach(
(command) =>
(output += `<a class="command font-weight-bold">${command.command}</a>: ${command.description}<br />`)
)

this.$store.dispatch('server/addEvent', { message: output, type: 'autocomplete' })

this.updateGcode(longestCommon, lastNewlineIndex, currentPosition)
}

updateGcode(text: string, start: number, end: number) {
this.gcode = this.gcode.substring(0, start + 1) + text + this.gcode.substring(end)
}
}
</script>

<style scoped>
.gcode-command-field {
font-family: 'Roboto Mono', monospace;
}
</style>
62 changes: 62 additions & 0 deletions src/components/mixins/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Vue from 'vue'
meteyou marked this conversation as resolved.
Show resolved Hide resolved
import Component from 'vue-class-component'
import { CommandHelp } from '@/store/printer/types'

@Component
export default class ConsoleMixin extends Vue {
get helplist(): CommandHelp[] {
return this.$store.state.printer.helplist ?? []
}

get consoleDirection() {
return this.$store.state.gui.console.direction ?? 'table'
}

get hideWaitTemperatures(): boolean {
return this.$store.state.gui.console.hideWaitTemperatures
}

set hideWaitTemperatures(newVal) {
this.$store.dispatch('gui/saveSetting', { name: 'console.hideWaitTemperatures', value: newVal })
}

get hideTlCommands(): boolean {
return this.$store.state.gui.console.hideWaitTemperatures
meteyou marked this conversation as resolved.
Show resolved Hide resolved
}

set hideTlCommands(newVal) {
this.$store.dispatch('gui/saveSetting', { name: 'console.hideTlCommands', value: newVal })
}

get customFilters() {
return this.$store.state.gui.console.consolefilters ?? {}
}

get autoscroll(): boolean {
return this.$store.state.gui.console.autoscroll ?? true
}

set autoscroll(newVal) {
this.$store.dispatch('gui/saveSetting', { name: 'console.autoscroll', value: newVal })
}

get rawOutput(): boolean {
return this.$store.state.gui.console.rawOutput ?? false
}

set rawOutput(newVal) {
this.$store.dispatch('gui/saveSetting', { name: 'console.rawOutput', value: newVal })
}

get lastCommands(): string[] {
return this.$store.state.gui.gcodehistory.entries ?? []
}

toggleFilter(filter: string): void {
this.$store.dispatch('gui/updateConsoleFilter', filter)
}

clearConsole() {
this.$store.dispatch('gui/console/clear')
}
}
Loading
Loading