Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/json5-1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-cooke authored Jul 7, 2024
2 parents 8fbdb5d + 84d0413 commit de6729a
Show file tree
Hide file tree
Showing 11 changed files with 733 additions and 97 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/odd-cougars-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prosemirror-docx': minor
---

Add tip tap default marks for bold and italic
5 changes: 5 additions & 0 deletions .changeset/silent-years-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prosemirror-docx': minor
---

Upgrade docx to 8.5.0
5 changes: 5 additions & 0 deletions .changeset/yellow-doors-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prosemirror-docx': minor
---

Exposes table row and cell docx run options via the state.table helper
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release

on:
push:
branches:
- main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Setup Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 20.x

- name: Install Dependencies
run: yarn

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: yarn release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
if: github.actor != 'dependabot[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v1
- uses: actions/setup-node@v4
with:
node-version: 14.x
- uses: actions/checkout@v2
- uses: actions/cache@v2
node-version: 20.x
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: 'node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
"test:clear": "jest --clearCache",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.json",
"lint:format": "prettier --check \"src/**/*.ts\"",
"lint:format:fix": "prettier --write \"src/**/*.ts\""
"lint:format:fix": "prettier --write \"src/**/*.ts\"",
"release": "yarn build && yarn changeset publish"
},
"dependencies": {
"@changesets/cli": "^2.27.7",
"buffer-image-size": "^0.6.4",
"docx": "^7.3.0",
"docx": "^8.5.0",
"prosemirror-model": "^1.18.1"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export const defaultMarks: MarkSerializer = {
strong() {
return { bold: true };
},
italic() {
return { italics: true };
},
bold() {
return { bold: true };
},
link() {
// Note, this is handled specifically in the serializer
// Word treats links more like a Node rather than a mark
Expand Down
46 changes: 35 additions & 11 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
InternalHyperlink,
SimpleField,
FootnoteReferenceRun,
IImageOptions,
ITableOptions,
ITableRowOptions,
} from 'docx';
import sizeOf from 'buffer-image-size';
import { createNumbering, NumberingStyles } from './numbering';
Expand Down Expand Up @@ -244,21 +247,28 @@ export class DocxSerializerState {
// not sure what this actually is, seems to be close for 8.5x11
maxImageWidth = MAX_IMAGE_WIDTH;

image(src: string, widthPercent = 70, align: AlignOptions = 'center') {
image(
src: string,
widthPercent = 70,
align: AlignOptions = 'center',
imageRunOpts?: IImageOptions,
) {
const buffer = this.options.getImageBuffer(src);
const dimensions = sizeOf(buffer);
const aspect = dimensions.height / dimensions.width;
const width = this.maxImageWidth * (widthPercent / 100);
this.current.push(
new ImageRun({
...imageRunOpts,
data: buffer,
transformation: {
...(imageRunOpts?.transformation || {}),
width,
height: width * aspect,
},
}),
);
let alignment: AlignmentType;
let alignment: string;
switch (align) {
case 'right':
alignment = AlignmentType.RIGHT;
Expand All @@ -270,38 +280,52 @@ export class DocxSerializerState {
alignment = AlignmentType.CENTER;
}
this.addParagraphOptions({
alignment,
// TODO: fix
alignment: alignment as any,
});
}

table(node: Node) {
table(
node: Node,
opts: {
getCellOptions?: (cell: Node) => ITableCellOptions;
getRowOptions?: (row: Node) => Omit<ITableRowOptions, 'children'>;
tableOptions?: Omit<ITableOptions, 'rows'>;
} = {},
) {
const { getCellOptions, getRowOptions, tableOptions } = opts;
const actualChildren = this.children;
const rows: TableRow[] = [];
node.content.forEach(({ content: rowContent }) => {
node.content.forEach((row) => {
const cells: TableCell[] = [];
// Check if all cells are headers in this row
let tableHeader = true;
rowContent.forEach((cell) => {
row.content.forEach((cell) => {
if (cell.type.name !== 'table_header') {
tableHeader = false;
}
});
// This scales images inside of tables
this.maxImageWidth = MAX_IMAGE_WIDTH / rowContent.childCount;
rowContent.forEach((cell) => {
this.maxImageWidth = MAX_IMAGE_WIDTH / row.content.childCount;
row.content.forEach((cell) => {
this.children = [];
this.renderContent(cell);
const tableCellOpts: Mutable<ITableCellOptions> = { children: this.children };
const colspan = cell.attrs.colspan ?? 1;
const rowspan = cell.attrs.rowspan ?? 1;
if (colspan > 1) tableCellOpts.columnSpan = colspan;
if (rowspan > 1) tableCellOpts.rowSpan = rowspan;
cells.push(new TableCell(tableCellOpts));
cells.push(
new TableCell({
...tableCellOpts,
...(getCellOptions?.(cell) || {}),
}),
);
});
rows.push(new TableRow({ children: cells, tableHeader }));
rows.push(new TableRow({ ...(getRowOptions?.(row) || {}), children: cells, tableHeader }));
});
this.maxImageWidth = MAX_IMAGE_WIDTH;
const table = new Table({ rows });
const table = new Table({ ...tableOptions, rows });
actualChildren.push(table);
// If there are multiple tables, this seperates them
actualChildren.push(new Paragraph(''));
Expand Down
Loading

0 comments on commit de6729a

Please sign in to comment.