Skip to content

Commit 999433e

Browse files
author
Orta Therox
authored
Merge pull request #920 from microsoft/play_fixes
Playground fixes
2 parents 3d88cb9 + e20a411 commit 999433e

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

packages/playground/src/index.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,19 @@ export const setupPlayground = (
154154
}, 300)
155155
})
156156

157+
// If you set this to true, then the next time the playground would
158+
// have set the user's hash it would be skipped - used for setting
159+
// the text in examples
160+
let suppressNextTextChangeForHashChange = false
161+
157162
// Sets the URL and storage of the sandbox string
158163
const playgroundDebouncedMainFunction = () => {
159164
const alwaysUpdateURL = !localStorage.getItem("disable-save-on-type")
160165
if (alwaysUpdateURL) {
166+
if (suppressNextTextChangeForHashChange) {
167+
suppressNextTextChangeForHashChange = false
168+
return
169+
}
161170
const newURL = sandbox.createURLQueryWithCompilerOptions(sandbox)
162171
window.history.replaceState({}, "", newURL)
163172
}
@@ -177,6 +186,9 @@ export const setupPlayground = (
177186
if (model && plugin.modelChangedDebounce) plugin.modelChangedDebounce(sandbox, model, container)
178187
})
179188

189+
const skipInitiallySettingHash = document.location.hash && document.location.hash.includes("example/")
190+
if (!skipInitiallySettingHash) playgroundDebouncedMainFunction()
191+
180192
// Setup working with the existing UI, once it's loaded
181193

182194
// Versions of TypeScript
@@ -274,6 +286,7 @@ export const setupPlayground = (
274286
}
275287
}
276288
}
289+
return false
277290
}
278291
})
279292

@@ -293,22 +306,31 @@ export const setupPlayground = (
293306
}
294307
}
295308

296-
// Set up some key commands
297-
sandbox.editor.addAction({
309+
const shareAction = {
298310
id: "copy-clipboard",
299311
label: "Save to clipboard",
300312
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S],
301313

302314
contextMenuGroupId: "run",
303315
contextMenuOrder: 1.5,
304316

305-
run: function (ed) {
317+
run: function () {
306318
window.navigator.clipboard.writeText(location.href.toString()).then(
307319
() => ui.flashInfo(i("play_export_clipboard")),
308320
(e: any) => alert(e)
309321
)
310322
},
311-
})
323+
}
324+
325+
const shareButton = document.getElementById("share-button")!
326+
shareButton.onclick = e => {
327+
e.preventDefault()
328+
shareAction.run()
329+
return false
330+
}
331+
332+
// Set up some key commands
333+
sandbox.editor.addAction(shareAction)
312334

313335
sandbox.editor.addAction({
314336
id: "run-js",
@@ -335,6 +357,7 @@ export const setupPlayground = (
335357

336358
const isJS = sandbox.config.useJavaScript
337359
ui.flashInfo(i(isJS ? "play_run_js" : "play_run_ts"))
360+
return false
338361
}
339362
}
340363

@@ -391,6 +414,15 @@ export const setupPlayground = (
391414
}
392415
settingsToggle.parentElement!.classList.toggle("open")
393416
}
417+
418+
settingsToggle.addEventListener("keydown", e => {
419+
const isOpen = settingsToggle.parentElement!.classList.contains("open")
420+
if (e.keyCode === 9 && isOpen) {
421+
const result = document.querySelector(".playground-options li input") as any
422+
result.focus()
423+
e.preventDefault()
424+
}
425+
})
394426
}
395427

396428
// Support grabbing examples from the location hash
@@ -409,16 +441,6 @@ export const setupPlayground = (
409441
localStorage.setItem("examples-seen", JSON.stringify(seen))
410442
}
411443

412-
// Set the menu to be the same section as this current example
413-
// this happens behind the scene and isn't visible till you hover
414-
// const sectionTitle = example.path[0]
415-
// const allSectionTitles = document.getElementsByClassName('section-name')
416-
// for (const title of allSectionTitles) {
417-
// if (title.textContent === sectionTitle) {
418-
// title.onclick({})
419-
// }
420-
// }
421-
422444
const allLinks = document.querySelectorAll("example-link")
423445
// @ts-ignore
424446
for (const link of allLinks) {
@@ -428,8 +450,10 @@ export const setupPlayground = (
428450
}
429451

430452
document.title = "TypeScript Playground - " + example.title
453+
suppressNextTextChangeForHashChange = true
431454
sandbox.setText(code)
432455
} else {
456+
suppressNextTextChangeForHashChange = true
433457
sandbox.setText("// There was an issue getting the example, bad URL? Check the console in the developer tools")
434458
}
435459
})
@@ -596,14 +620,12 @@ export const setupPlayground = (
596620
export type Playground = ReturnType<typeof setupPlayground>
597621

598622
const redirectTabPressTo = (element: HTMLElement, container: HTMLElement | undefined, query: string) => {
599-
// element.style.backgroundColor = "red"
600623
element.addEventListener("keydown", e => {
601624
if (e.keyCode === 9) {
602625
const host = container || document
603626
const result = host.querySelector(query) as any
604627
if (!result) throw new Error(`Expected to find a result for keydown`)
605628
result.focus()
606-
console.log(result)
607629
e.preventDefault()
608630
}
609631
})

packages/typescriptlang-org/src/copy/en/playground.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const playCopy = {
1111
play_downloading_version: "Version...", // when loading
1212
play_toolbar_run: "Run",
1313
play_toolbar_export: "Export",
14+
play_toolbar_share: "Share",
1415
play_sidebar_js: ".JS",
1516
play_sidebar_dts: ".D.TS",
1617
play_sidebar_errors: "Errors",
@@ -27,7 +28,8 @@ export const playCopy = {
2728
"Disable changing the URL when you type.",
2829
play_sidebar_plugins: "Plugins",
2930
play_sidebar_featured_plugins: "Featured Plugins",
30-
play_sidebar_plugins_options_external: "3rd Party Plugins <a href='https://www.npmjs.com/search?q=keywords:playground-plugin'>from npm</a>",
31+
play_sidebar_plugins_options_external:
32+
"3rd Party Plugins <a href='https://www.npmjs.com/search?q=keywords:playground-plugin'>from npm</a>",
3133
play_sidebar_plugins_options_external_warning:
3234
"Warning: Code from plugins comes from third-parties.",
3335
play_sidebar_plugins_options_modules: "Custom npm Modules",

packages/typescriptlang-org/src/templates/pages/community.tsx

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ export const Comm: React.FC<Props> = props => {
4141
const intl = useIntl()
4242
const i = createInternational<typeof comCopy>(intl)
4343

44-
// Make an array of all the meetups which share a rough location
45-
const meetupByArea = {}
46-
meetups.forEach(meetup => {
47-
const area = meetup.meetup.continentish
48-
if (meetupByArea[area]) {
49-
meetupByArea[area].push(meetup.meetup)
50-
} else {
51-
meetupByArea[area] = [meetup.meetup]
52-
}
53-
});
54-
5544
return (
5645
<Layout title={i("com_layout_title")} description={i("com_layout_description")} lang={props.pageContext.lang} allSitePage={props.data.allSitePage}>
5746
<div className="raised main-content-block container community" style={{ marginTop: "80px" }}>
@@ -193,23 +182,21 @@ export const Comm: React.FC<Props> = props => {
193182
<div className="raised main-content-block container community">
194183
<h3 className="centered-highlight">Meetups</h3>
195184
<div className="events">
196-
{Object.keys(meetupByArea)
197-
.map(area => (
198-
<>
199-
<h4>{area}</h4>
200-
<div className="callouts">
201-
{meetupByArea[area].map(meetup => (
202-
<Col className="callout">
203-
<img src={require("../../assets/community/meetup-logos/" + meetup.image)} className="icon img-square" alt={"logo of " + meetup.title} />
204-
<div>
205-
<h4 className="community-callout-headline">{meetup.title}</h4>
206-
<div className="text">{meetup.country}<br /><a rel="noopener" target="blank" href={meetup.url}>Website</a>{meetup.twitter ? <a rel="noopener" target="blank" href={meetup.twitter}>Twitter</a> : null}</div>
207-
</div>
208-
</Col>
209-
))}
185+
186+
<div className="callouts">
187+
{meetups.sort().map(m => m.meetup).map(meetup => (
188+
<Col className="callout">
189+
<img src={require("../../assets/community/meetup-logos/" + meetup.image)} className="icon img-square" alt={"logo of " + meetup.title} />
190+
<div>
191+
<h4 className="community-callout-headline">{meetup.title}</h4>
192+
<div className="text">{meetup.country}<br />
193+
<a rel="noopener" target="blank" href={meetup.url} title={"Website for " + meetup.title}>Website</a>
194+
{meetup.twitter ? <a rel="noopener" target="blank" href={meetup.twitter} title={"Twitter page for " + meetup.title}>Twitter</a> : null}
195+
</div>
210196
</div>
211-
</>
197+
</Col>
212198
))}
199+
</div>
213200
</div>
214201
</div>
215202
</Layout >

packages/typescriptlang-org/src/templates/pages/css/community.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
width: 48%;
3737
display: flex;
3838
line-height: 1.4rem;
39+
40+
a {
41+
margin-right: 8px;
42+
}
3943
}
4044

4145
h3 {

packages/typescriptlang-org/src/templates/play.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ const Play: React.FC<Props> = (props) => {
226226
<li><a href="#" onClick={() => playground.exporter.openProjectInStackBlitz()} aria-label={i("play_export_stackblitz")} >{i("play_export_stackblitz")}</a></li>
227227
</ul>
228228
</li>
229+
<li><a id="share-button" href="#" role="button">{i("play_toolbar_share")}</a></li>
229230
</ul>
231+
230232
<ul className="right">
231233
<li><a id="sidebar-toggle" aria-label="Hide Sidebar" href="#">&#x21E5;</a></li>
232234
</ul>

0 commit comments

Comments
 (0)