Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getNext = (prev: string): string =>
typeof edit === 'string'
? edit
: applyEdits(
prev,
// TODO(chris): remove `.slice()` (which guards against mutation) once
// https://github.com/Microsoft/node-jsonc-parser/pull/12 is merged in.
setProperty(prev, edit.path.slice(), edit.value, {
tabSize: 2,
insertSpaces: true,
eol: '\n',
})
)
if (isInPage) {
run: editor => {
eventLogger.log('SiteConfigurationActionExecuted', { id })
editor.focus()
editor.pushUndoStop()
const { edits, selectText, cursorOffset } = run(editor.getValue())
const monacoEdits = toMonacoEdits(model, edits)
let selection: monaco.Selection | undefined
if (typeof selectText === 'string') {
const afterText = jsonc.applyEdits(editor.getValue(), edits)
let offset = afterText.slice(edits[0].offset).indexOf(selectText)
if (offset !== -1) {
offset += edits[0].offset
if (typeof cursorOffset === 'number') {
selection = monaco.Selection.fromPositions(
getPositionAt(afterText, offset + cursorOffset),
getPositionAt(afterText, offset + cursorOffset)
)
} else {
selection = monaco.Selection.fromPositions(
getPositionAt(afterText, offset),
getPositionAt(afterText, offset + selectText.length)
)
}
}
}
export function modifyJSONC(text: string, path: jsonc.JSONPath, f: (oldValue: jsonc.Node | undefined) => any): any {
const old = jsonc.findNodeAtLocation(jsonc.parseTree(text), path)
return jsonc.applyEdits(
text,
jsoncEdit.setProperty(text, path, f(old), {
eol: '\n',
insertSpaces: true,
tabSize: 2,
})
)
}
run: editor => {
eventLogger.log('SiteConfigurationActionExecuted', { id })
editor.focus()
editor.pushUndoStop()
const { edits, selectText, cursorOffset } = run(editor.getValue())
const monacoEdits = toMonacoEdits(model, edits)
let selection: monaco.Selection | undefined
if (typeof selectText === 'string') {
const afterText = jsonc.applyEdits(editor.getValue(), edits)
let offset = afterText.slice(edits[0].offset).indexOf(selectText)
if (offset !== -1) {
offset += edits[0].offset
if (typeof cursorOffset === 'number') {
selection = monaco.Selection.fromPositions(
getPositionAt(afterText, offset + cursorOffset),
getPositionAt(afterText, offset + cursorOffset)
)
} else {
selection = monaco.Selection.fromPositions(
getPositionAt(afterText, offset),
getPositionAt(afterText, offset + selectText.length)
)
}
}
}
export async function editSiteConfig(
gqlClient: GraphQLClient,
...edits: ((contents: string) => jsonc.Edit[])[]
): Promise<{ destroy: ResourceDestructor; result: boolean }> {
const origConfig = await fetchSiteConfiguration(gqlClient).toPromise()
let newContents = origConfig.configuration.effectiveContents
for (const editFn of edits) {
newContents = jsonc.applyEdits(newContents, editFn(newContents))
}
return {
result: await updateSiteConfiguration(gqlClient, origConfig.configuration.id, newContents).toPromise(),
destroy: async () => {
const c = await fetchSiteConfiguration(gqlClient).toPromise()
await updateSiteConfiguration(
gqlClient,
c.configuration.id,
origConfig.configuration.effectiveContents
).toPromise()
},
}
}
const createChangeHandler = (prop: string) => (event: React.ChangeEvent<{ value: string }>) =>
onChange(
jsonc.applyEdits(
campaignArguments,
setProperty(campaignArguments, [prop], event.target.value, defaultFormattingOptions)
)
)
storage.getSync(storageItems => {
const format = { tabSize: 2, insertSpaces: true, eol: '\n' }
if (edit) {
storageItems.clientSettings = applyEdits(
storageItems.clientSettings,
setProperty(storageItems.clientSettings, edit.path, edit.value, format)
)
} else if (typeof enabled === 'boolean') {
storageItems.clientSettings = applyEdits(
storageItems.clientSettings,
setProperty(storageItems.clientSettings, ['extensions', extensionID], enabled, format)
)
} else if (remove) {
storageItems.clientSettings = applyEdits(
storageItems.clientSettings,
removeProperty(storageItems.clientSettings, ['extensions', extensionID], format)
)
}
storage.setSync(storageItems, () => {
update.next(undefined)
})
})
return update