-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
562 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import io | ||
import os | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from pikepdf import Pdf | ||
|
||
from papermerge.core.models import Page | ||
from papermerge.core.schemas import DocumentVersion as PyDocVer | ||
from papermerge.core.schemas.pages import PageAndRotOp | ||
from papermerge.core.storage import abs_path, get_storage_instance | ||
|
||
|
||
def apply_pages_op(items: List[PageAndRotOp]): | ||
pages = Page.objects.filter( | ||
pk__in=[item.page.id for item in items] | ||
) | ||
old_version = pages.first().document_version | ||
|
||
doc = old_version.document | ||
new_version = doc.version_bump( | ||
page_count=len(items) | ||
) | ||
|
||
reorder_pdf_pages( | ||
old_version=old_version, | ||
new_version=new_version, | ||
items=items | ||
) | ||
|
||
|
||
def reorder_pdf_pages( | ||
old_version: PyDocVer, | ||
new_version: PyDocVer, | ||
items: List[PageAndRotOp] | ||
): | ||
src = Pdf.open(abs_path(old_version.document_path.url)) | ||
|
||
dst = Pdf.new() | ||
|
||
for item in items: | ||
page = src.pages.p(item.page.number) | ||
dst.pages.append(page) | ||
|
||
dirname = os.path.dirname( | ||
abs_path(new_version.document_path.url) | ||
) | ||
os.makedirs(dirname, exist_ok=True) | ||
dst.save(abs_path(new_version.document_path.url)) | ||
|
||
|
||
def reuse_ocr_data(uuid_map) -> None: | ||
storage_instance = get_storage_instance() | ||
|
||
for src_uuid, dst_uuid in uuid_map.items(): | ||
storage_instance.copy_page( | ||
src=Path("pages", src_uuid), | ||
dst=Path("pages", dst_uuid) | ||
) | ||
|
||
|
||
def reuse_text_field( | ||
old_version: PyDocVer, | ||
new_version: PyDocVer, | ||
page_map: list | ||
) -> None: | ||
streams = collect_text_streams( | ||
version=old_version, | ||
# list of old_version page numbers | ||
page_numbers=[item[1] for item in page_map] | ||
) | ||
|
||
# updates page.text fields and document_version.text field | ||
new_version.update_text_field(streams) | ||
|
||
|
||
def collect_text_streams( | ||
version: PyDocVer, | ||
page_numbers: list[int] | ||
) -> list[io.StringIO]: | ||
""" | ||
Returns list of texts of given page numbers from specified document version | ||
Each page's text is wrapped as io.StringIO instance. | ||
""" | ||
pages_map = {page.number: page for page in version.pages.all()} | ||
|
||
result = [ | ||
io.StringIO(pages_map[number].text) | ||
for number in page_numbers | ||
] | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Page(BaseModel): | ||
id: UUID | ||
number: int | ||
|
||
|
||
class PageAndRotOp(BaseModel): | ||
page: Page | ||
ccw: int = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Button } from "react-bootstrap" | ||
import UnappliedPageOpChanges from "./unapplied_page_op_changes" | ||
|
||
type Args = { | ||
unapplied_page_op_changes: boolean; | ||
onApplyPageOpChanges: () => void; | ||
} | ||
|
||
export default function ActionPanel({ | ||
unapplied_page_op_changes, | ||
onApplyPageOpChanges | ||
}: Args) { | ||
return ( | ||
<div className="action-panel"> | ||
<Button className="rounded-0 m-1" variant="success"> | ||
<i className="bi bi-pencil-square me-1"></i>Rename | ||
</Button> | ||
<Button className="rounded-0 m-1" variant="success"> | ||
<i className="bi bi-cloud-download me-1"></i>Download | ||
</Button> | ||
{ | ||
unapplied_page_op_changes && | ||
<UnappliedPageOpChanges | ||
onClick={onApplyPageOpChanges}/>} | ||
</div> | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
ui/src/components/viewer/action_panel/unapplied_page_op_changes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useState } from "react"; | ||
import { Button, Spinner } from "react-bootstrap" | ||
|
||
|
||
type Args = { | ||
onClick: () => void; | ||
} | ||
|
||
export default function UnappliedPageOpChanges({onClick}: Args) { | ||
const [inProgress, setInProgress] = useState(false); | ||
|
||
const onLocalClick = () => { | ||
setInProgress(true); | ||
onClick(); | ||
} | ||
|
||
return ( | ||
<span className="unapplied-page-op-changes"> | ||
Unapplied pages operations detected | ||
<Button disabled={inProgress} | ||
className="rounded-0 m-1" variant="success" | ||
onClick={onLocalClick}> | ||
{inProgress && <Spinner />} Apply | ||
</Button> | ||
</span> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { Page } from "./page"; | ||
import type { PageType } from "types" | ||
import type { PageAndRotOp } from "types" | ||
|
||
type Args = { | ||
pages: Array<PageType>; | ||
items: Array<PageAndRotOp>; | ||
current_page_number: number; | ||
} | ||
|
||
export function PagesPanel({pages, current_page_number}: Args) { | ||
export function PagesPanel({items, current_page_number}: Args) { | ||
return ( | ||
<div className='pages-panel flex-grow-1'> | ||
{pages.map(page => <Page | ||
key={page.id} | ||
page={page} | ||
scroll_into_view={page.number == current_page_number}/>)} | ||
{items.map(item => <Page | ||
key={item.page.id} | ||
item={item} | ||
scroll_into_view={item.page.number == current_page_number}/>)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const PAGE_ID = "page_id"; |
13 changes: 13 additions & 0 deletions
13
ui/src/components/viewer/thumbnails_panel/page_thumbnail.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
.page-thumbnail { | ||
border-top: 3px solid #666; | ||
border-bottom: 3px solid #666; | ||
} | ||
|
||
.borderline-top { | ||
border-top-color: blue; | ||
} | ||
|
||
.borderline-bottom { | ||
border-bottom-color: blue; | ||
} |
Oops, something went wrong.