Skip to content

Commit 9db3779

Browse files
Allow for / search functionality (#3746)
1 parent 4ac402c commit 9db3779

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

edit-page.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,69 @@
8383
});
8484
});
8585
}
86+
87+
const SEARCH_TRIGGER_SELECTORS = [
88+
'button[aria-label*="Search"]',
89+
'[data-commandk-trigger]',
90+
'[data-cmdk-trigger]',
91+
'.DocSearch-Button',
92+
'button[class*="search"]',
93+
];
94+
95+
function isEditableElement(element) {
96+
if (!element) {
97+
return false;
98+
}
99+
100+
const tagName = element.tagName;
101+
return (
102+
element.isContentEditable ||
103+
tagName === 'INPUT' ||
104+
tagName === 'TEXTAREA' ||
105+
element.getAttribute('role') === 'textbox'
106+
);
107+
}
108+
109+
function triggerSearchPalette() {
110+
const syntheticEvent = new KeyboardEvent('keydown', {
111+
key: 'k',
112+
code: 'KeyK',
113+
ctrlKey: true,
114+
metaKey: true,
115+
bubbles: true,
116+
cancelable: true
117+
});
118+
119+
window.dispatchEvent(syntheticEvent);
120+
121+
for (const selector of SEARCH_TRIGGER_SELECTORS) {
122+
const candidate = document.querySelector(selector);
123+
if (candidate) {
124+
candidate.click();
125+
return;
126+
}
127+
}
128+
}
129+
130+
window.addEventListener(
131+
'keydown',
132+
function(event) {
133+
if (
134+
event.defaultPrevented ||
135+
event.repeat ||
136+
event.key !== '/' ||
137+
event.altKey ||
138+
event.metaKey ||
139+
event.ctrlKey ||
140+
event.shiftKey ||
141+
isEditableElement(event.target)
142+
) {
143+
return;
144+
}
145+
146+
event.preventDefault();
147+
triggerSearchPalette();
148+
},
149+
true
150+
);
86151
})();

0 commit comments

Comments
 (0)