Skip to content

Commit

Permalink
feat(preprocess/contextmenu): simplify matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri committed Sep 8, 2024
1 parent c0bb5d8 commit e2fd1e6
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,11 @@ func exposeAPIs_main(input string) string {
return fmt.Sprintf("%s[Spicetify.ContextMenuV2.renderItems(),%s].flat()", submatches[1], submatches[2])
})

croppedInput := utils.FindFirstMatch(input, `\({menu:[^,]+,.*?triggerRef:[^,]+,[^}]*}\).*value:"contextmenu"`)[0]

reactRef := utils.FindFirstMatch(croppedInput, `([\w_$]+)\.useRef`)
var react, menu, trigger, target string
if len(reactRef) == 1 {
react = reactRef[0]
} else {
react = reactRef[1]
}

menuCandidates := utils.FindMatch(croppedInput, `menu:([\w_$]+)`)
menu = menuCandidates[0][1]
trigger = utils.FindFirstMatch(croppedInput, `trigger:([\w_$]+)`)[1]
target = utils.FindFirstMatch(croppedInput, `triggerRef:([\w_$]+)`)[1]
croppedInput := utils.FindFirstMatch(input, `.*value:"contextmenu"`)[0]
react := utils.FindLastMatch(croppedInput, `([a-zA-Z_\$][\w\$]*)\.useRef`)[1]
menu := utils.FindLastMatch(croppedInput, `menu:([\w_$]+)`)[1]
trigger := utils.FindLastMatch(croppedInput, `trigger:([\w_$]+)`)[1]
target := utils.FindLastMatch(croppedInput, `triggerRef:([\w_$]+)`)[1]

utils.Replace(&input, `\(0,([\w_$]+)\.jsx\)\([\w_$]+\.[\w_$]+,\{value:"contextmenu"[^\}]+\}\)\}\)`, func(submatches ...string) string {
return fmt.Sprintf("(0,%s.jsx)((Spicetify.ContextMenuV2._context||(Spicetify.ContextMenuV2._context=%s.createContext(null))).Provider,{value:{props:%s?.props,trigger:%s,target:%s},children:%s})", submatches[1], react, menu, trigger, target, submatches[0])
Expand Down

0 comments on commit e2fd1e6

Please sign in to comment.