mirror of
https://github.com/overleaf/overleaf.git
synced 2025-03-13 13:26:58 +00:00
[cm6] Add analytics for bold and italic shortcuts (#13785)
GitOrigin-RevId: 62f2f9976778083b7ca6566f79e3d055585e7bb7
This commit is contained in:
parent
48e758a5fe
commit
e86c5dc137
5 changed files with 42 additions and 6 deletions
|
@ -1,5 +1,10 @@
|
||||||
import { EditorView } from '@codemirror/view'
|
import { EditorView } from '@codemirror/view'
|
||||||
import { EditorSelection, EditorState, SelectionRange } from '@codemirror/state'
|
import {
|
||||||
|
EditorSelection,
|
||||||
|
EditorState,
|
||||||
|
SelectionRange,
|
||||||
|
TransactionSpec,
|
||||||
|
} from '@codemirror/state'
|
||||||
import {
|
import {
|
||||||
ensureSyntaxTree,
|
ensureSyntaxTree,
|
||||||
foldedRanges,
|
foldedRanges,
|
||||||
|
@ -408,7 +413,10 @@ function bubbleUpRange(
|
||||||
return range
|
return range
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toggleRanges(command: string) {
|
export function toggleRanges(
|
||||||
|
command: string,
|
||||||
|
annotations?: TransactionSpec['annotations']
|
||||||
|
) {
|
||||||
/* There are a number of situations we need to handle in this function.
|
/* There are a number of situations we need to handle in this function.
|
||||||
* In the following examples, the selection range is marked within <>
|
* In the following examples, the selection range is marked within <>
|
||||||
|
|
||||||
|
@ -745,7 +753,7 @@ export function toggleRanges(command: string) {
|
||||||
// Shouldn't happen, but default to just wrapping the content
|
// Shouldn't happen, but default to just wrapping the content
|
||||||
return wrapRangeInCommand(view.state, range, command)
|
return wrapRangeInCommand(view.state, range, command)
|
||||||
}),
|
}),
|
||||||
{ scrollIntoView: true }
|
{ scrollIntoView: true, annotations }
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@ import { toolbarPanel } from './toolbar/toolbar-panel'
|
||||||
import { geometryChangeEvent } from './geometry-change-event'
|
import { geometryChangeEvent } from './geometry-change-event'
|
||||||
import { isSplitTestEnabled } from '../../../utils/splitTestUtils'
|
import { isSplitTestEnabled } from '../../../utils/splitTestUtils'
|
||||||
import { completionLogger } from './completion-logger'
|
import { completionLogger } from './completion-logger'
|
||||||
|
import { shortcutLogger } from './shortcut-logger'
|
||||||
|
|
||||||
const moduleExtensions: Array<() => Extension> = importOverleafModules(
|
const moduleExtensions: Array<() => Extension> = importOverleafModules(
|
||||||
'sourceEditorExtensions'
|
'sourceEditorExtensions'
|
||||||
|
@ -130,6 +131,7 @@ export const createExtensions = (options: Record<string, any>): Extension[] => [
|
||||||
highlightActiveLineGutter(),
|
highlightActiveLineGutter(),
|
||||||
inlineBackground(options.visual.visual),
|
inlineBackground(options.visual.visual),
|
||||||
completionLogger,
|
completionLogger,
|
||||||
|
shortcutLogger,
|
||||||
codemirrorDevTools(),
|
codemirrorDevTools(),
|
||||||
exceptionLogger(),
|
exceptionLogger(),
|
||||||
// CodeMirror extensions provided by modules
|
// CodeMirror extensions provided by modules
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { ViewPlugin } from '@codemirror/view'
|
||||||
|
import { emitShortcutEvent } from './toolbar/utils/analytics'
|
||||||
|
import { runShortcut } from '../languages/latex/shortcuts'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom view plugin that watches for transactions with the `runShortcut` annotation,
|
||||||
|
* and logs the shortcut name for analytics.
|
||||||
|
*/
|
||||||
|
export const shortcutLogger = ViewPlugin.define(view => {
|
||||||
|
return {
|
||||||
|
update(update) {
|
||||||
|
for (const tr of update.transactions) {
|
||||||
|
const action = tr.annotation(runShortcut)
|
||||||
|
if (action) {
|
||||||
|
emitShortcutEvent(view, action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
|
@ -18,3 +18,7 @@ export function emitToolbarEvent(view: EditorView, command: string) {
|
||||||
export function emitCompletionEvent(view: EditorView, command: string) {
|
export function emitCompletionEvent(view: EditorView, command: string) {
|
||||||
emitCommandEvent(view, 'codemirror-completion-event', command)
|
emitCommandEvent(view, 'codemirror-completion-event', command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function emitShortcutEvent(view: EditorView, command: string) {
|
||||||
|
emitCommandEvent(view, 'codemirror-shortcut-event', command)
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Prec } from '@codemirror/state'
|
import { Annotation, Prec } from '@codemirror/state'
|
||||||
import { keymap } from '@codemirror/view'
|
import { keymap } from '@codemirror/view'
|
||||||
import { toggleRanges } from '../../commands/ranges'
|
import { toggleRanges } from '../../commands/ranges'
|
||||||
|
|
||||||
|
@ -9,14 +9,16 @@ export const shortcuts = () => {
|
||||||
key: 'Ctrl-b',
|
key: 'Ctrl-b',
|
||||||
mac: 'Mod-b',
|
mac: 'Mod-b',
|
||||||
preventDefault: true,
|
preventDefault: true,
|
||||||
run: toggleRanges('\\textbf'),
|
run: toggleRanges('\\textbf', runShortcut.of('toggle-bold')),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'Ctrl-i',
|
key: 'Ctrl-i',
|
||||||
mac: 'Mod-i',
|
mac: 'Mod-i',
|
||||||
preventDefault: true,
|
preventDefault: true,
|
||||||
run: toggleRanges('\\textit'),
|
run: toggleRanges('\\textit', runShortcut.of('toggle-italic')),
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const runShortcut = Annotation.define<string>()
|
||||||
|
|
Loading…
Reference in a new issue