Skip to content

Commit 249230f

Browse files
committed
feat(writer-mode): add 'open in editor' button
1 parent 8476169 commit 249230f

File tree

12 files changed

+84
-51
lines changed

12 files changed

+84
-51
lines changed

build/render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ const legacyManifest = JSON.parse(
2323
);
2424

2525
/**
26-
* @param {import("@rari").BuiltPage} page
26+
* @param {import("@fred").PartialContext} context
2727
*/
28-
export async function render(page) {
29-
return await distRender(page.url, page, {
28+
export async function render(context) {
29+
return await distRender(context.url, context, {
3030
client: clientManifest,
3131
legacy: legacyManifest,
3232
});

build/server-worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parentPort, workerData } from "node:worker_threads";
44
import "source-map-support/register.js";
55

66
/** @type {import("./types.js").WorkerData} */
7-
const { reqPath, page, compilationStats } = workerData;
7+
const { reqPath, context, compilationStats } = workerData;
88

99
const ssrStats = compilationStats.find((x) => x.name === "ssr");
1010
if (!ssrStats) {
@@ -27,7 +27,7 @@ if (!indexModulePath) {
2727
try {
2828
/** @type {import("../entry.ssr.js")} */
2929
const indexModule = await import(indexModulePath);
30-
const html = await indexModule?.render(reqPath, page, {
30+
const html = await indexModule?.render(reqPath, context, {
3131
client: compilationStats.find((x) => x.name === "client") || {},
3232
legacy: compilationStats.find((x) => x.name === "legacy") || {},
3333
});

build/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { BuiltPage } from "@mdn/rari";
1+
import { PartialContext } from "@fred";
22
import { StatsCompilation } from "@rspack/core";
33

44
export interface WorkerData {
55
reqPath: string;
6-
page: BuiltPage;
6+
context: PartialContext;
77
compilationStats: StatsCompilation[];
88
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { LitElement, html } from "lit";
2+
3+
import { L10nMixin } from "../../l10n/mixin.js";
4+
5+
import "../button/element.js";
6+
7+
export class MDNWriterOpenEditor extends L10nMixin(LitElement) {
8+
static properties = {
9+
filepath: { type: String },
10+
};
11+
12+
constructor() {
13+
super();
14+
this.filepath = "";
15+
}
16+
17+
async _open() {
18+
const params = new URLSearchParams({
19+
filepath: this.filepath,
20+
});
21+
await fetch(`/_open?${params}`);
22+
}
23+
24+
render() {
25+
return html`<mdn-button @click=${this._open} variant="plain">
26+
${this.l10n`Open in editor`}
27+
</mdn-button>`;
28+
}
29+
}
30+
31+
customElements.define("mdn-writer-open-editor", MDNWriterOpenEditor);

components/writer-toolbar/server.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.writer-toolbar {
2+
display: flex;
3+
4+
flex-wrap: wrap;
5+
26
margin-top: 1.5rem;
37

48
background: var(--color-background-primary);

components/writer-toolbar/server.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { html } from "lit";
1+
import { html, nothing } from "lit";
22

33
import { Button } from "../button/server.js";
44
import { ServerComponent } from "../server/index.js";
@@ -9,13 +9,19 @@ export class WriterToolbar extends ServerComponent {
99
*/
1010
render(context) {
1111
const prodUrl = new URL(context.url, "https://developer.mozilla.org");
12+
const { folder, filename } = context.doc.source;
1213

1314
return html`<div class="writer-toolbar">
1415
${Button.render(context, {
1516
label: context.l10n`View on MDN`,
1617
href: prodUrl.toString(),
1718
variant: "plain",
1819
})}
20+
${context.localServer
21+
? html`<mdn-writer-open-editor
22+
filepath=${`${folder}/${filename}`}
23+
></mdn-writer-open-editor>`
24+
: nothing}
1925
</div>`;
2026
}
2127
}

entry.ssr.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ for (const [name, def] of customElements.__definitions) {
4040

4141
/**
4242
* @param {string} path
43-
* @param {import("@rari").BuiltPage} page
43+
* @param {import("@fred").PartialContext} partialContext
4444
* @param {import("@fred").CompilationStats} compilationStats
4545
*/
46-
export async function render(path, page, compilationStats) {
46+
export async function render(path, partialContext, compilationStats) {
4747
const locale = path.split("/")[1] || "en-US";
4848
if (locale === "qa") {
4949
path = path.replace("/qa/", "/en-US/");
@@ -52,7 +52,7 @@ export async function render(path, page, compilationStats) {
5252
const context = {
5353
path,
5454
...(await addFluent(locale)),
55-
...page,
55+
...partialContext,
5656
};
5757

5858
/** @type {import("./components/server/types.js").AsyncLocalStorageContents} */

0 commit comments

Comments
 (0)