Skip to content

Commit

Permalink
fix: implement quick keys Ctrl+F and Ctrl+H to open the find dialog w…
Browse files Browse the repository at this point in the history
…hilst editing a key or value
  • Loading branch information
josdejong committed Mar 10, 2022
1 parent a8abe71 commit e608486
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ type RenderValueProps = {
onPatch: (patch: JSONPatchDocument, newSelection: Selection | null) => void
onPasteJson: (pastedJson: { path: Path; contents: JSON }) => void
onSelect: (selection: Selection) => void
onFind: (findAndReplace: boolean) => void
}
type ValueNormalization = {
Expand Down
15 changes: 14 additions & 1 deletion src/lib/components/controls/EditableDiv.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
export let shortText = false
export let onChange
export let onCancel
export let onFind
export let onPaste = noop
export let onValueClass = () => ''
Expand Down Expand Up @@ -68,7 +69,7 @@
function handleValueKeyDown(event) {
event.stopPropagation()
const combo = keyComboFromEvent(event)
const combo = keyComboFromEvent(event).replace(/^Command\+/, 'Ctrl+')
if (combo === 'Escape') {
// cancel changes (needed to prevent triggering a change onDestroy)
Expand All @@ -84,6 +85,16 @@
const newValue = getDomValue()
onChange(newValue, UPDATE_SELECTION.NEXT_INSIDE)
}
if (combo === 'Ctrl+F') {
event.preventDefault()
onFind(false)
}
if (combo === 'Ctrl+H') {
event.preventDefault()
onFind(true)
}
}
function handleValuePaste(event) {
Expand All @@ -109,6 +120,8 @@
closed = true
if (newValue !== value) {
onChange(newValue, UPDATE_SELECTION.SELF)
} else {
onCancel()
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/components/modes/treemode/JSONKey.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import { SELECTION_TYPE } from '$lib/logic/selection'
import SearchResultHighlighter from './highlight/SearchResultHighlighter.svelte'
import EditableDiv from '../../controls/EditableDiv.svelte'
import { addNewLineSuffix } from '$lib/utils/domUtils.js'
import { addNewLineSuffix } from '$lib/utils/domUtils'
import { UPDATE_SELECTION } from '../../../constants.js'
export let path
export let key
export let readOnly
export let onUpdateKey
export let onFind
export let selection
/** @type {ValueNormalization} */
Expand Down Expand Up @@ -71,6 +72,7 @@
shortText
onChange={handleChangeValue}
onCancel={handleCancelChange}
{onFind}
/>
{:else}
<div data-type="selectable-key" class={getKeyClass(key)} on:dblclick={handleKeyDoubleClick}>
Expand Down
7 changes: 6 additions & 1 deletion src/lib/components/modes/treemode/JSONNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import JSONValue from './JSONValue.svelte'
import { singleton } from './singleton.js'
import ValidationError from './ValidationError.svelte'
import { STATE_ENFORCE_STRING } from '$lib/constants.js'
import { STATE_ENFORCE_STRING } from '$lib/constants'
// eslint-disable-next-line no-undef-init
export let value
Expand All @@ -52,6 +52,7 @@
export let onInsert
export let onExpand
export let onSelect
export let onFind
export let onPasteJson
export let onRenderValue
export let onContextMenu
Expand Down Expand Up @@ -386,6 +387,7 @@
{onInsert}
{onExpand}
{onSelect}
{onFind}
{onPasteJson}
{onExpandSection}
{onRenderValue}
Expand Down Expand Up @@ -511,6 +513,7 @@
{onInsert}
{onExpand}
{onSelect}
{onFind}
{onPasteJson}
{onExpandSection}
{onRenderValue}
Expand All @@ -529,6 +532,7 @@
searchResult={searchResult?.[key]?.[STATE_SEARCH_PROPERTY]}
onUpdateKey={handleUpdateKey}
{onSelect}
{onFind}
/>
{#if !readOnly && selectionObj && selectionObj.type === SELECTION_TYPE.KEY && !selectionObj.edit && isEqual(selectionObj.focusPath, path.concat(key))}
<ContextMenuButton selected={true} {onContextMenu} />
Expand Down Expand Up @@ -567,6 +571,7 @@
searchResult={searchResult ? searchResult[STATE_SEARCH_VALUE] : undefined}
{onPatch}
{onSelect}
{onFind}
{onPasteJson}
{onRenderValue}
/>
Expand Down
6 changes: 4 additions & 2 deletions src/lib/components/modes/treemode/JSONValue.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<svelte:options immutable={true} />

<script>
import { SELECTION_TYPE } from '$lib/logic/selection.js'
import { SELECTION_TYPE } from '$lib/logic/selection'
import { isEqual } from 'lodash-es'
export let path
Expand All @@ -19,6 +19,7 @@
export let onPatch
export let onPasteJson
export let onSelect
export let onFind
export let onRenderValue
$: isSelected =
Expand All @@ -40,7 +41,8 @@
searchResult,
onPatch,
onPasteJson,
onSelect
onSelect,
onFind
})
</script>

Expand Down
37 changes: 19 additions & 18 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,22 @@
}
}
/**
* @param {boolean} findAndReplace
*/
function openFind(findAndReplace) {
debug('openFind', { findAndReplace })
showSearch = false
showReplace = false
tick().then(() => {
// trick to make sure the focus goes to the search box
showSearch = true
showReplace = findAndReplace
})
}
function handleExpandSection(path, section) {
debug('handleExpandSection', path, section)
Expand Down Expand Up @@ -1684,28 +1700,12 @@
if (combo === 'Ctrl+F') {
event.preventDefault()
showSearch = false
showReplace = false
tick().then(() => {
// trick to make sure the focus goes to the search box
showSearch = true
showReplace = false
})
openFind(false)
}
if (combo === 'Ctrl+H') {
event.preventDefault()
showSearch = false
showReplace = false
tick().then(() => {
// trick to make sure the focus goes to the search box
showSearch = true
showReplace = true
})
openFind(true)
}
if (combo === 'Ctrl+Z') {
Expand Down Expand Up @@ -2027,6 +2027,7 @@
onInsert={handleInsert}
onExpand={handleExpand}
onSelect={handleSelect}
onFind={openFind}
onPasteJson={handlePasteJson}
onExpandSection={handleExpandSection}
{onRenderValue}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/plugins/value/components/EditableValue.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
export let onPatch
export let onPasteJson
export let onSelect
export let onFind
function convert(value) {
return enforceString ? value : stringConvert(value)
Expand Down Expand Up @@ -80,5 +81,6 @@
onChange={handleChangeValue}
onCancel={handleCancelChange}
onPaste={handlePaste}
{onFind}
onValueClass={handleOnValueClass}
/>
5 changes: 3 additions & 2 deletions src/lib/plugins/value/renderValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function renderValue({
normalization,
onPatch,
onPasteJson,
onSelect
onSelect,
onFind
}) {
const renderers = []

Expand All @@ -40,7 +41,7 @@ export function renderValue({
if (isEditing) {
renderers.push({
component: EditableValue,
props: { path, value, enforceString, normalization, onPatch, onPasteJson, onSelect }
props: { path, value, enforceString, normalization, onPatch, onPasteJson, onSelect, onFind }
})
}

Expand Down

0 comments on commit e608486

Please sign in to comment.