Skip to content

Commit

Permalink
feat: added print styles and js for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
yndx-birman committed Oct 18, 2023
1 parent b7ca82d commit 4bc6ad7
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 44 deletions.
64 changes: 41 additions & 23 deletions esbuild/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@ const common = {
};

(async function buildCss() {
await build({
...common,
entryPoints: ['src/scss/yfm.scss'],
outfile: 'dist/css/yfm.css',
format: 'iife',
plugins: [
sassPlugin({
async transform(source) {
const {css} = await postcss([
autoprefixer({cascade: false}),
postcssPresetEnv({stage: 0}),
]).process(source, {from: undefined});

return css;
},
}),
],
});
const plugins = [
sassPlugin({
async transform(source) {
const {css} = await postcss([
autoprefixer({cascade: false}),
postcssPresetEnv({stage: 0}),
]).process(source, {from: undefined});

return css;
},
}),
];

await Promise.all([
build({
...common,
entryPoints: ['src/scss/yfm.scss'],
outfile: 'dist/css/yfm.css',
format: 'iife',
plugins,
}),
build({
...common,
entryPoints: ['src/scss/print.scss'],
outfile: 'dist/css/print.css',
format: 'iife',
plugins,
})
]);

await build({
...common,
Expand All @@ -48,11 +59,18 @@ const common = {
})();

(async function buildJs() {
await build({
...common,
entryPoints: ['src/js/index.ts'],
outfile: 'dist/js/yfm.js',
});
await Promise.all([
build({
...common,
entryPoints: ['src/js/index.ts'],
outfile: 'dist/js/yfm.js',
}),
build({
...common,
entryPoints: ['src/js/print/index.ts'],
outfile: 'dist/js/print.js',
})
]);

await build({
...common,
Expand Down
1 change: 1 addition & 0 deletions src/js/print/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './table';
62 changes: 62 additions & 0 deletions src/js/print/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const Selector = {
WRAPPED_TABLE: '.pdf .yfm-table-container table',
TABLE: '.pdf table',
};
const Padding = {
BOTTOM: 20,
};

function resizeElement(element: HTMLElement) {
const availableWidth = element.parentElement?.offsetWidth;
const contentWidth = (element.firstElementChild as HTMLElement).offsetWidth;

if (!availableWidth) {
return;
}

const needScale = contentWidth > availableWidth;

if (needScale) {
const scale = availableWidth / contentWidth;

element.style.transform = `scale(${scale})`;
}

element.parentElement.style.height = `${
element.getBoundingClientRect().height + Padding.BOTTOM
}px`;
}

function resizeElements() {
document.querySelectorAll(Selector.WRAPPED_TABLE).forEach((element) => {
resizeElement(element as HTMLElement);
});
}

function wrapTables() {
const tables = document.querySelectorAll(Selector.TABLE);

for (let i = 0; i < tables.length; i++) {
const table = tables[i];

const parent = table.parentNode;

if (!parent) {
continue;
}

const wrapper = document.createElement('div');
parent.insertBefore(wrapper, table);
wrapper.appendChild(table);
wrapper.classList.add('yfm-table-container');
}
}

if (typeof document !== 'undefined') {
window.addEventListener('load', () => {
wrapTables();
resizeElements();
});
}

export {};
20 changes: 0 additions & 20 deletions src/scss/_print.scss

This file was deleted.

6 changes: 6 additions & 0 deletions src/scss/print.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import 'print/code';
@import 'print/common';
@import 'print/cut';
@import 'print/note';
@import 'print/table';
@import 'print/tabs';
9 changes: 9 additions & 0 deletions src/scss/print/code.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@media print {
.yfm {
.yfm-clipboard {
& > pre > code {
white-space: pre-wrap;
}
}
}
}
13 changes: 13 additions & 0 deletions src/scss/print/common.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@media print {
.yfm {
.yfm-page__delimeter {
display: none;
}

h1, h2 {
&[data-original-article] {
page-break-before: always;
}
}
}
}
18 changes: 18 additions & 0 deletions src/scss/print/cut.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@media print {
.yfm {
.yfm-cut {
&-title {
padding-left: 0;

&:before {
display: none;
}
}

&-content {
height: auto;
padding: 5px 0 15px 30px;
}
}
}
}
7 changes: 7 additions & 0 deletions src/scss/print/note.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@media print {
.yfm {
.yfm-note {
page-break-inside: avoid;
}
}
}
24 changes: 24 additions & 0 deletions src/scss/print/table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.yfm {
.yfm-table-container {
position: relative;

& > table {
overflow: initial;
position: absolute;
top: 0;
left: 0;
max-width: initial;
transform-origin: top left;

thead {
display: table-row-group;
}

th, td {
page-break-inside: avoid;
white-space: pre-wrap;
text-overflow: initial;
}
}
}
}
20 changes: 20 additions & 0 deletions src/scss/print/tabs.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@media print {
.yfm {
.yfm-tab {
&-list {
display: none;
}

&-panel:before {
content: attr(data-title);
margin-bottom: -1px;
margin-right: 20px;
border-bottom: 2px solid transparent;
line-height: 33px;
font-weight: 700;
outline: 0;
cursor: pointer;
}
}
}
}
1 change: 0 additions & 1 deletion src/scss/yfm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@import 'anchor';
@import 'highlight';
@import 'code';
@import 'print';
@import 'cut';
@import 'file';
@import 'term';
Expand Down

0 comments on commit 4bc6ad7

Please sign in to comment.