Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

[WIP] Implement Custom Command #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/options/CustomCommands/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Custom Commands",
"options": [
{
"type": "customcommands",
"key": "customCommands",
"default": []
}
]
}
11 changes: 11 additions & 0 deletions src/options/CustomCommands/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Custom Commands",
"profiles": [
{
"name": "default",
"options": {
"customCommands": []
}
}
]
}
9 changes: 9 additions & 0 deletions src/options/CustomCommands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getAttributes } from 'lib/util'

export default (options, config) => {
console.error('CustomCommand/index.js called', options, config)
const backgroundOptions = {}
const clientOptions = {}
const errors = {}
return { backgroundOptions, clientOptions, errors }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Component, h } from 'preact'
import TextArea from '../TextArea'

const DEFAULT = {
source: 'document.body.style.backgroundColor = "blue"'
}

class CustomCommand extends Component {
render ({ command, onChange }) {
return (
<li>
<p>This is a command</p>
<TextArea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, Widgets are meant to be standalone containers representing a single option. Although code reuse is nice, I think it's important to be able to change the TextArea Widget independently of the CustomCommand Widget.

label='Source'
value={command.source}
onChange={s => onChange({ source: s, ...command })}
/>
</li>
)
}
}

export default class CustomCommands extends Component {
render ({ key, value, onChange, ...rest }) {
console.log('CustomCommands', key, value, rest)
return (
<li>
<ul>
{value &&
value.map((cmd, i, cmds) => {
let cmdChange = cmd => {
let updated = cmds.slice()
updated[i] = cmd
onChange(updated)
}
return <CustomCommand command={cmd} onChange={cmdChange} />
})}
</ul>
<button onClick={e => onChange([...value, DEFAULT])}>
New custom command
</button>
</li>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ToggleButton from './ToggleButton'
import Switch from './Switch'
import Checkbox from './Checkbox'
import Color from './Color'
import CustomCommands from './CustomCommands'
import Text from './Text'
import Number from './Number'
import TextArea from './TextArea'
Expand Down Expand Up @@ -34,6 +35,8 @@ function optionWidgetByType (type) {
return Switch
case 'color':
return Color
case 'customcommands':
return CustomCommands
case 'text':
return Text
case 'number':
Expand Down
6 changes: 4 additions & 2 deletions src/storage/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export async function storageUpdateProcedure (previousVersion) {
await storageSet({ ready: true })
} catch (e) {
console.error(
'FATAL ERROR: UPDATE FAILED. DELETE THEN REINSTALL THE EXTENSION.',
'FATAL ERROR: UPDATE FAILED. WIPING STORAGE AND REINITIALIZING.',
e
)
await storageClear()
await storageInstallProcedure()
}
}

Expand Down Expand Up @@ -228,7 +230,7 @@ async function deleteConfig () {
/**
* Accounts for the case where a built-in profile introduced in an update conflicts with
* a custom profile already installed. Renames the custom profile, active profile, and action
* if a conflic is detected.
* if a conflict is detected.
*/
async function renameCustomProfilesAndOptions () {
const {
Expand Down
4 changes: 3 additions & 1 deletion src/storage/transform.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import transformGeneral from 'options/General'
import transformKeybindings from 'options/Keybindings'
import transformAppearance from 'options/Appearance'
import transformCustomCommands from 'options/CustomCommands'
const transforms = {
General: transformGeneral,
Keybindings: transformKeybindings,
Appearance: transformAppearance
Appearance: transformAppearance,
CustomCommands: transformCustomCommands
}
export const categories = Object.keys(transforms)

Expand Down