Skip to content

Commit

Permalink
feat: new colors
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko committed Sep 13, 2023
1 parent 590819e commit 7203489
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/includer/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function code(text: string) {
return EOL + ['```', text, '```'].join(EOL) + EOL;
}

function method(text: string) {
return `${text.toUpperCase()} {.openapi__method}`
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
function table(data: any[][]) {
const [names, ...rest] = data;
Expand Down Expand Up @@ -93,6 +97,6 @@ function anchor(ref: string) {
return link(ref, `#${slugify(ref).toLowerCase()}`);
}

export {meta, list, link, title, body, mono, bold, table, code, cut, block, page, tabs, anchor};
export {meta, list, link, title, body, mono, bold, table, code, cut, block, page, tabs, anchor, method};

export default {meta, list, link, title, body, mono, bold, table, code, cut, block, tabs, anchor};
export default {meta, list, link, title, body, mono, bold, table, code, cut, block, tabs, anchor, method};
42 changes: 25 additions & 17 deletions src/includer/generators/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {JSONSchema6} from 'json-schema';
import stringify from 'json-stringify-safe';

import {meta, page, block, title, body, table, code, cut, tabs, bold} from './common';
import {meta, page, block, title, body, table, code, cut, tabs, bold, method} from './common';
import {
INFO_TAB_NAME,
SANDBOX_TAB_NAME,
Expand Down Expand Up @@ -110,30 +110,36 @@ function sandbox({
}

function request(data: Endpoint) {
const {path, method, servers} = data;
const requestTableCols = ['method', 'url'];
const {path, method: type, servers} = data;
let description: string | undefined;

const hrefs = block(servers.map(({url}) => code(url + '/' + path)));
const url = block(servers.map(({url}) => code(url + '/' + path)));

const requestTableRow = [code(method.toUpperCase()), hrefs];
const requestTableRow = [method(type), `${url}`];

if (servers.every((server: Server) => server.description)) {
requestTableCols.push('description');

const descriptions = block(servers.map(({description}) => code(description as string)));

requestTableRow.push(descriptions);
if (servers.every((server: Server) => server.description)) {
description = block(servers.map(({description}) => description));
}

const requestTable = table([
requestTableCols,
requestTableRow,
]);
const requestTable = block([
'<div class="openapi__request__wrapper">',
`<div class="openapi__request" style="--method: var(--dc-openapi-methods-${type})">`,
...requestTableRow,
'</div>',
'</div>'
])

return block([
const result = [
title(2)(REQUEST_SECTION_NAME),
requestTable,
]);
]

if (description) {
result.push(`${description}{.openapi__request__description}`)
}

return block(result);
}

function parameters(allRefs: Refs, pagePrintedRefs: Set<string>, params?: Parameters) {
Expand Down Expand Up @@ -195,7 +201,7 @@ function openapiBody(allRefs: Refs, pagePrintedRefs: Set<string>, obj?: Schema)
const {type = 'schema', schema} = obj;
const sectionTitle = title(4)('Body');

let result = [
let result: any[] = [
sectionTitle,
];

Expand Down Expand Up @@ -274,9 +280,11 @@ function response(allRefs: Refs, visited: Set<string>, resp: Response) {
}

return block([
`<div class="openapi__response__code__${resp.code}">`,
title(2)(header),
body(resp.description),
resp.schemas?.length && block(resp.schemas.map((s) => openapiBody(allRefs, visited, s))),
'</div>'
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/includer/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export type Tag = {

export type Endpoints = Endpoint[];


export type Endpoint = {
id: string;
operationId?: string;
Expand All @@ -172,6 +171,7 @@ export type Endpoint = {
security: Security[];
noindex?: boolean;
hidden?: boolean;
deprecated?: boolean;
};

export type Specification = {
Expand Down
103 changes: 103 additions & 0 deletions src/runtime/index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,108 @@
.openapi {
--dc-openapi-methods-post: rgba(50, 186, 118, 0.5);
--dc-openapi-methods-put: rgba(50, 186, 118, 0.3);
--dc-openapi-methods-get: rgba(54, 151, 241, 0.5);
--dc-openapi-methods-patch: rgba(255, 190, 92, 0.9);
--dc-openapi-methods-delete: rgba(255, 0, 61, 0.5);

--dc-openapi-status-code-ok: rgb(48, 170, 110);
--dc-openapi-status-code-redirect: rgb(52, 139, 220);
--dc-openapi-status-code-client: rgb(233, 3, 58);
--dc-openapi-status-code-server: rgb(255, 190, 92);

--dc-openapi-highlight: rgb(233, 174, 86);

&__required {
color: var(--yc-color-text-danger);
}

& #request {
margin-top: 0;
}

& .openapi__deprecated {
text-decoration: line-through;
}

& &__request {
display: flex;
align-items: center;
overflow-y: scroll;
width: 100%;

&__wrapper {
position: relative;
}

& * {
margin: 0;
}

& > .openapi__method {
background-color: var(--method);
padding: 8px 16px;
color: #fff;
border-radius: 5px 0 0 5px;
}

& > .yfm-clipboard {
border: 2px solid var(--method);
border-radius: 0 5px 5px 0;
white-space: nowrap;
overflow: visible;
position: initial;

code {
padding: 6px 20px 6px 6px;
}

pre, code {
border-radius: 0;
overflow: visible;
}

svg {
display: block;
opacity: 0;
transition: opacity 100ms;
position: absolute;
right: -8px;
top: 10px;
}

&:hover svg {
opacity: 1;
}
}

}

& h3 {
transition: color 100ms;

&.highlight {
color: var(--dc-openapi-highlight);
}
}

& .openapi__request__description {
margin: 20px 0 0 0;
font-size: 1.1em;
}

& h2[id^="2"] {
color: var(--dc-openapi-status-code-ok);
}

& h2[id^="3"] {
color: var(--dc-openapi-status-code-redirect);
}

& h2[id^="4"] {
color: var(--dc-openapi-status-code-client);
}

& h2[id^="5"] {
color: var(--dc-openapi-status-code-server);
}
}
34 changes: 29 additions & 5 deletions src/runtime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import React, {useEffect, useState} from 'react';
import {createPortal} from 'react-dom';
import {unescape} from 'html-escaper';
import React, { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { unescape } from 'html-escaper';

import {Sandbox} from './sandbox';
import { Sandbox } from './sandbox';

import './index.scss';

export const Runtime: React.FC = () => {
const [sandbox, setSandbox] = useState<HTMLElement | null>(null);

useEffect(() => {
document.addEventListener('click', (event: any) => {
if (!event?.target?.closest('.openapi')) {
return;
}

const id = event?.target?.hash;

if (!id) {
return;
}

const anchor = document.querySelector(id);

if (anchor.classList.contains('highlight')) {
return;
}

anchor.classList.toggle('highlight');

setTimeout(() => anchor.classList.toggle('highlight'), 1_000)
})
}, []);

useEffect(() => {
setSandbox(document.querySelector<HTMLElement>('.yfm-sandbox-js'));
});
Expand All @@ -20,7 +44,7 @@ export const Runtime: React.FC = () => {
try {
const props = JSON.parse(unescape(sandbox.dataset.props));

return createPortal(<Sandbox { ...props } />, sandbox);
return createPortal(<Sandbox {...props} />, sandbox);
} catch (error) {
console.log(error);

Expand Down

0 comments on commit 7203489

Please sign in to comment.