Phase 3 (Organize + robust save) + Phase 4 (Edit content); update docs
Engine: - organize.rs: build_document(plan) core — insert/delete/rotate/crop/extract/merge/split - watermark.rs: watermark + header/footer via PdfPages::watermark, CID Unicode fonts - qpdf.rs: QPDF CLI wrapper — repair corrupt files, re-encrypt after edits, ensure_openable - edit.rs: MOAT — list_objects + apply_edits (SetText VN, Transform, Delete, Add/ReplaceImage) - Tests: organize/watermark/qpdf_safety/edit roundtrip — 43/43 engine tests green UI (Tauri): "Tổ chức trang" mode (grid + drag-reorder + multi-select), "Sửa nội dung" mode (WYSIWYG object overlay, edit-in-place), global undo/redo, robust file open. Corpus: add encrypted.pdf + corrupt-truncated.pdf fixtures. Docs: mark Phase 1-4 done in roadmap; document QPDF setup + engine module layout in 05-dev-setup; add 10-13 phase summaries & user-test checklists; README status.
This commit is contained in:
+528
-2
@@ -193,6 +193,12 @@ fn pick_pdf(app: tauri::AppHandle) -> Option<String> {
|
||||
.map(|fp| fp.to_string())
|
||||
}
|
||||
|
||||
/// Hộp thoại chọn thư mục (dùng cho Tách file — chọn nơi lưu các phần).
|
||||
#[tauri::command]
|
||||
fn pick_dir(app: tauri::AppHandle) -> Option<String> {
|
||||
app.dialog().file().blocking_pick_folder().map(|fp| fp.to_string())
|
||||
}
|
||||
|
||||
/// Hộp thoại lưu file PDF; trả về đường dẫn (hoặc None nếu huỷ).
|
||||
#[tauri::command]
|
||||
fn pick_save_pdf(app: tauri::AppHandle) -> Option<String> {
|
||||
@@ -205,7 +211,7 @@ fn pick_save_pdf(app: tauri::AppHandle) -> Option<String> {
|
||||
}
|
||||
|
||||
/// Hộp chữ nhật đơn giản (1 quad) dùng cho `AnnotSpecDto.quads`.
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct RectDto {
|
||||
left: f32,
|
||||
@@ -322,6 +328,509 @@ fn initial_file() -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Phase 3: Tổ chức trang ====================
|
||||
|
||||
/// Nếu PDFium không mở thẳng được `path` (file hỏng xref/trailer), dùng QPDF
|
||||
/// repair rồi trả về đường dẫn file tạm đã sửa — frontend mở lại bằng path đó.
|
||||
/// Trả về chính `path` nếu mở thẳng được (không cần repair).
|
||||
#[tauri::command]
|
||||
fn ensure_openable(path: String, password: Option<String>) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let usable = ff_engine::ensure_openable(&pdfium, std::path::Path::new(&path), password.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(usable.to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct PagePlanEntryDto {
|
||||
/// "existing" | "blank"
|
||||
kind: String,
|
||||
source: Option<String>,
|
||||
src_index: Option<u16>,
|
||||
width_pt: Option<f32>,
|
||||
height_pt: Option<f32>,
|
||||
#[serde(default)]
|
||||
rotation_delta: i32,
|
||||
crop: Option<RectDto>,
|
||||
}
|
||||
|
||||
fn plan_entry_from_dto(d: PagePlanEntryDto) -> Result<ff_engine::PagePlanEntry, String> {
|
||||
let page = match d.kind.as_str() {
|
||||
"existing" => ff_engine::PageSource::Existing {
|
||||
source: d.source.map(PathBuf::from),
|
||||
src_index: d.src_index.ok_or("thiếu srcIndex cho khe 'existing'")?,
|
||||
},
|
||||
"blank" => ff_engine::PageSource::Blank {
|
||||
width_pt: d.width_pt.ok_or("thiếu widthPt cho khe 'blank'")?,
|
||||
height_pt: d.height_pt.ok_or("thiếu heightPt cho khe 'blank'")?,
|
||||
},
|
||||
other => return Err(format!("loại khe trang không hỗ trợ: {other}")),
|
||||
};
|
||||
Ok(ff_engine::PagePlanEntry {
|
||||
page,
|
||||
rotation_delta: d.rotation_delta,
|
||||
crop: d.crop.map(|c| ff_engine::Rect { left: c.left, bottom: c.bottom, right: c.right, top: c.top }),
|
||||
})
|
||||
}
|
||||
|
||||
fn plan_entry_to_dto(e: &ff_engine::PagePlanEntry) -> PagePlanEntryDto {
|
||||
let (kind, source, src_index, width_pt, height_pt) = match &e.page {
|
||||
ff_engine::PageSource::Existing { source, src_index } => {
|
||||
("existing".to_string(), source.as_ref().map(|p| p.to_string_lossy().into_owned()), Some(*src_index), None, None)
|
||||
}
|
||||
ff_engine::PageSource::Blank { width_pt, height_pt } => {
|
||||
("blank".to_string(), None, None, Some(*width_pt), Some(*height_pt))
|
||||
}
|
||||
};
|
||||
PagePlanEntryDto {
|
||||
kind,
|
||||
source,
|
||||
src_index,
|
||||
width_pt,
|
||||
height_pt,
|
||||
rotation_delta: e.rotation_delta,
|
||||
crop: e.crop.map(|c| RectDto { left: c.left, bottom: c.bottom, right: c.right, top: c.top }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Plan "giữ nguyên" — điểm khởi đầu cho UI Tổ chức trang khi mở 1 file.
|
||||
#[tauri::command]
|
||||
fn organize_identity_plan(path: String, password: Option<String>) -> Result<Vec<PagePlanEntryDto>, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let plan = ff_engine::identity_plan(&pdfium, std::path::Path::new(&path), password.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(plan.iter().map(plan_entry_to_dto).collect())
|
||||
}
|
||||
|
||||
/// Áp dụng 1 plan trang đầy đủ (chèn/xoá/xoay/đảo/thay/crop) — dựng tài liệu
|
||||
/// mới ghi ra `output`. `mainInput` dùng cho các khe `source: null`.
|
||||
#[tauri::command]
|
||||
fn organize_apply(
|
||||
main_input: String,
|
||||
plan: Vec<PagePlanEntryDto>,
|
||||
output: String,
|
||||
password: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
let mapped: Vec<ff_engine::PagePlanEntry> =
|
||||
plan.into_iter().map(plan_entry_from_dto).collect::<Result<_, String>>()?;
|
||||
ff_engine::build_document(
|
||||
&pdfium,
|
||||
std::path::Path::new(&main_input),
|
||||
&mapped,
|
||||
std::path::Path::new(&output),
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Trích các trang `pages` (0-based, giữ thứ tự truyền vào) ra file mới.
|
||||
#[tauri::command]
|
||||
fn organize_extract(
|
||||
input: String,
|
||||
pages: Vec<u16>,
|
||||
output: String,
|
||||
password: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
ff_engine::extract_pages(
|
||||
&pdfium,
|
||||
std::path::Path::new(&input),
|
||||
&pages,
|
||||
std::path::Path::new(&output),
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Trộn nhiều file PDF thành 1, theo đúng thứ tự `files`.
|
||||
#[tauri::command]
|
||||
fn organize_merge(files: Vec<String>, output: String) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
let paths: Vec<PathBuf> = files.into_iter().map(PathBuf::from).collect();
|
||||
ff_engine::merge_files(&pdfium, &paths, std::path::Path::new(&output)).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Tách `input` thành nhiều file, mỗi file tối đa `pages_per_file` trang.
|
||||
/// Trả về danh sách đường dẫn đã ghi.
|
||||
#[tauri::command]
|
||||
fn organize_split(
|
||||
input: String,
|
||||
pages_per_file: u16,
|
||||
out_dir: String,
|
||||
base_name: String,
|
||||
password: Option<String>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let outputs = ff_engine::split_by_page_count(
|
||||
&pdfium,
|
||||
std::path::Path::new(&input),
|
||||
pages_per_file,
|
||||
std::path::Path::new(&out_dir),
|
||||
&base_name,
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(outputs.into_iter().map(|p| p.to_string_lossy().into_owned()).collect())
|
||||
}
|
||||
|
||||
/// Dựng tạm tài liệu theo `plan` HIỆN TẠI (chưa lưu) ra 1 file tạm — dùng làm
|
||||
/// "input" cho Watermark/Header-Footer khi người dùng đang có thay đổi tổ
|
||||
/// chức trang (chèn/xoá/đảo/xoay/crop) chưa lưu, để 2 thao tác đó thấy đúng
|
||||
/// trạng thái đang xem trên lưới, không phải file gốc còn trên đĩa.
|
||||
#[tauri::command]
|
||||
fn organize_materialize(
|
||||
main_input: String,
|
||||
plan: Vec<PagePlanEntryDto>,
|
||||
password: Option<String>,
|
||||
) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let mapped: Vec<ff_engine::PagePlanEntry> =
|
||||
plan.into_iter().map(plan_entry_from_dto).collect::<Result<_, String>>()?;
|
||||
let tmp = std::env::temp_dir().join(format!("ff_materialized_{}.pdf", std::process::id()));
|
||||
ff_engine::build_document(
|
||||
&pdfium,
|
||||
std::path::Path::new(&main_input),
|
||||
&mapped,
|
||||
&tmp,
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(tmp.to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
fn parse_anchor(s: &str) -> Result<ff_engine::Anchor, String> {
|
||||
use ff_engine::Anchor::*;
|
||||
Ok(match s {
|
||||
"top-left" => TopLeft,
|
||||
"top-center" => TopCenter,
|
||||
"top-right" => TopRight,
|
||||
"middle-left" => MiddleLeft,
|
||||
"center" => Center,
|
||||
"middle-right" => MiddleRight,
|
||||
"bottom-left" => BottomLeft,
|
||||
"bottom-center" => BottomCenter,
|
||||
"bottom-right" => BottomRight,
|
||||
other => return Err(format!("vị trí neo không hợp lệ: {other}")),
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct WatermarkDto {
|
||||
text: String,
|
||||
font_size: f32,
|
||||
color: [u8; 4],
|
||||
#[serde(default)]
|
||||
bold: bool,
|
||||
#[serde(default)]
|
||||
italic: bool,
|
||||
#[serde(default)]
|
||||
rotation_deg: f32,
|
||||
anchor: String,
|
||||
#[serde(default)]
|
||||
pages: Vec<u16>,
|
||||
}
|
||||
|
||||
/// Thêm watermark văn bản vào `input`, ghi ra `output`.
|
||||
#[tauri::command]
|
||||
fn watermark_add(
|
||||
input: String,
|
||||
spec: WatermarkDto,
|
||||
output: String,
|
||||
password: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
let anchor = parse_anchor(&spec.anchor)?;
|
||||
let ws = ff_engine::WatermarkSpec {
|
||||
text: spec.text,
|
||||
font_size: spec.font_size,
|
||||
color: spec.color,
|
||||
bold: spec.bold,
|
||||
italic: spec.italic,
|
||||
rotation_deg: spec.rotation_deg,
|
||||
anchor,
|
||||
pages: spec.pages,
|
||||
};
|
||||
ff_engine::add_watermark(&pdfium, std::path::Path::new(&input), &ws, std::path::Path::new(&output), password.as_deref())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct HeaderFooterDto {
|
||||
#[serde(default)]
|
||||
top_left: String,
|
||||
#[serde(default)]
|
||||
top_center: String,
|
||||
#[serde(default)]
|
||||
top_right: String,
|
||||
#[serde(default)]
|
||||
bottom_left: String,
|
||||
#[serde(default)]
|
||||
bottom_center: String,
|
||||
#[serde(default)]
|
||||
bottom_right: String,
|
||||
font_size: f32,
|
||||
color: [u8; 4],
|
||||
margin_pt: f32,
|
||||
#[serde(default)]
|
||||
bold: bool,
|
||||
#[serde(default)]
|
||||
italic: bool,
|
||||
#[serde(default)]
|
||||
date: String,
|
||||
#[serde(default)]
|
||||
pages: Vec<u16>,
|
||||
}
|
||||
|
||||
/// Thêm header/footer (gồm đánh số trang qua token {page}/{total}) vào `input`.
|
||||
#[tauri::command]
|
||||
fn header_footer_add(
|
||||
input: String,
|
||||
spec: HeaderFooterDto,
|
||||
output: String,
|
||||
password: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
let hf = ff_engine::HeaderFooterSpec {
|
||||
top_left: spec.top_left,
|
||||
top_center: spec.top_center,
|
||||
top_right: spec.top_right,
|
||||
bottom_left: spec.bottom_left,
|
||||
bottom_center: spec.bottom_center,
|
||||
bottom_right: spec.bottom_right,
|
||||
font_size: spec.font_size,
|
||||
color: spec.color,
|
||||
margin_pt: spec.margin_pt,
|
||||
bold: spec.bold,
|
||||
italic: spec.italic,
|
||||
date: spec.date,
|
||||
pages: spec.pages,
|
||||
};
|
||||
ff_engine::add_header_footer(&pdfium, std::path::Path::new(&input), &hf, std::path::Path::new(&output), password.as_deref())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn render_temp_page(pdfium: &pdfium_render::prelude::Pdfium, path: &std::path::Path, page: u16, width: u32) -> Result<String, String> {
|
||||
let rendered = ff_engine::render::render_page(pdfium, path, page, width, None).map_err(|e| e.to_string())?;
|
||||
let mut buf = Cursor::new(Vec::new());
|
||||
rendered.image.write_to(&mut buf, image::ImageFormat::Png).map_err(|e| e.to_string())?;
|
||||
let b64 = base64::engine::general_purpose::STANDARD.encode(buf.get_ref());
|
||||
Ok(format!("data:image/png;base64,{b64}"))
|
||||
}
|
||||
|
||||
/// Xem trước watermark trên 1 trang: áp vào file tạm rồi render, không sửa `input`.
|
||||
#[tauri::command]
|
||||
fn preview_watermark(input: String, page: u16, spec: WatermarkDto, width: u32) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let anchor = parse_anchor(&spec.anchor)?;
|
||||
let ws = ff_engine::WatermarkSpec {
|
||||
text: spec.text, font_size: spec.font_size, color: spec.color,
|
||||
bold: spec.bold, italic: spec.italic, rotation_deg: spec.rotation_deg,
|
||||
anchor, pages: vec![page],
|
||||
};
|
||||
let tmp = std::env::temp_dir().join(format!("ff_preview_wm_{}.pdf", std::process::id()));
|
||||
ff_engine::add_watermark(&pdfium, std::path::Path::new(&input), &ws, &tmp, None).map_err(|e| e.to_string())?;
|
||||
let result = render_temp_page(&pdfium, &tmp, page, width);
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
result
|
||||
}
|
||||
|
||||
/// Xem trước header/footer trên 1 trang: áp vào file tạm rồi render, không sửa `input`.
|
||||
#[tauri::command]
|
||||
fn preview_header_footer(input: String, page: u16, spec: HeaderFooterDto, width: u32) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let hf = ff_engine::HeaderFooterSpec {
|
||||
top_left: spec.top_left, top_center: spec.top_center, top_right: spec.top_right,
|
||||
bottom_left: spec.bottom_left, bottom_center: spec.bottom_center, bottom_right: spec.bottom_right,
|
||||
font_size: spec.font_size, color: spec.color, margin_pt: spec.margin_pt,
|
||||
bold: spec.bold, italic: spec.italic, date: spec.date, pages: vec![page],
|
||||
};
|
||||
let tmp = std::env::temp_dir().join(format!("ff_preview_hf_{}.pdf", std::process::id()));
|
||||
ff_engine::add_header_footer(&pdfium, std::path::Path::new(&input), &hf, &tmp, None).map_err(|e| e.to_string())?;
|
||||
let result = render_temp_page(&pdfium, &tmp, page, width);
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
result
|
||||
}
|
||||
|
||||
// ---------- Phase 4: Sửa nội dung (Edit) ----------
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ObjectInfoDto {
|
||||
index: u16,
|
||||
kind: String,
|
||||
rect: RectDto,
|
||||
text: Option<String>,
|
||||
font_name: Option<String>,
|
||||
font_size: Option<f32>,
|
||||
color: Option<[u8; 4]>,
|
||||
}
|
||||
|
||||
/// Liệt kê page object của 1 trang để UI vẽ overlay chỉnh sửa.
|
||||
#[tauri::command]
|
||||
fn edit_list_objects(path: String, page: u16, password: Option<String>) -> Result<Vec<ObjectInfoDto>, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let objs = ff_engine::list_objects(&pdfium, std::path::Path::new(&path), page, password.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(objs
|
||||
.into_iter()
|
||||
.map(|o| ObjectInfoDto {
|
||||
index: o.index,
|
||||
kind: o.kind.as_str().to_string(),
|
||||
rect: RectDto { left: o.rect.left, bottom: o.rect.bottom, right: o.rect.right, top: o.rect.top },
|
||||
text: o.text,
|
||||
font_name: o.font_name,
|
||||
font_size: o.font_size,
|
||||
color: o.color,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// 1 thao tác sửa nội dung (tagged theo field `op`). Field thừa cho mỗi loại để None/mặc định.
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct EditOpDto {
|
||||
/// "transform" | "setText" | "delete" | "replaceImage" | "addText" | "addImage"
|
||||
op: String,
|
||||
#[serde(default)]
|
||||
index: u16,
|
||||
#[serde(default)]
|
||||
dx: f32,
|
||||
#[serde(default)]
|
||||
dy: f32,
|
||||
#[serde(default = "one")]
|
||||
sx: f32,
|
||||
#[serde(default = "one")]
|
||||
sy: f32,
|
||||
#[serde(default)]
|
||||
text: String,
|
||||
font_size: Option<f32>,
|
||||
color: Option<[u8; 4]>,
|
||||
#[serde(default)]
|
||||
bold: bool,
|
||||
#[serde(default)]
|
||||
italic: bool,
|
||||
#[serde(default)]
|
||||
x: f32,
|
||||
#[serde(default)]
|
||||
y: f32,
|
||||
#[serde(default)]
|
||||
width_pt: f32,
|
||||
#[serde(default)]
|
||||
height_pt: f32,
|
||||
#[serde(default)]
|
||||
image_path: String,
|
||||
}
|
||||
|
||||
fn one() -> f32 {
|
||||
1.0
|
||||
}
|
||||
|
||||
fn edit_op_from_dto(d: EditOpDto) -> Result<ff_engine::EditOp, String> {
|
||||
Ok(match d.op.as_str() {
|
||||
"transform" => ff_engine::EditOp::Transform { index: d.index, dx: d.dx, dy: d.dy, sx: d.sx, sy: d.sy },
|
||||
"setText" => ff_engine::EditOp::SetText {
|
||||
index: d.index,
|
||||
text: d.text,
|
||||
font_size: d.font_size,
|
||||
color: d.color,
|
||||
bold: d.bold,
|
||||
italic: d.italic,
|
||||
},
|
||||
"delete" => ff_engine::EditOp::Delete { index: d.index },
|
||||
"replaceImage" => ff_engine::EditOp::ReplaceImage { index: d.index, image_path: d.image_path },
|
||||
"addText" => ff_engine::EditOp::AddText {
|
||||
x: d.x,
|
||||
y: d.y,
|
||||
text: d.text,
|
||||
font_size: d.font_size.unwrap_or(14.0),
|
||||
color: d.color.unwrap_or([0, 0, 0, 255]),
|
||||
bold: d.bold,
|
||||
italic: d.italic,
|
||||
},
|
||||
"addImage" => ff_engine::EditOp::AddImage {
|
||||
x: d.x,
|
||||
y: d.y,
|
||||
width_pt: d.width_pt,
|
||||
height_pt: d.height_pt,
|
||||
image_path: d.image_path,
|
||||
},
|
||||
other => return Err(format!("op sửa nội dung không hỗ trợ: {other}")),
|
||||
})
|
||||
}
|
||||
|
||||
/// Áp các thao tác sửa nội dung lên trang `page`, ghi ra `output`.
|
||||
#[tauri::command]
|
||||
fn edit_apply(
|
||||
input: String,
|
||||
page: u16,
|
||||
ops: Vec<EditOpDto>,
|
||||
output: String,
|
||||
password: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let pdfium = pdfium()?;
|
||||
let mapped: Vec<ff_engine::EditOp> = ops.into_iter().map(edit_op_from_dto).collect::<Result<_, String>>()?;
|
||||
ff_engine::apply_edits(
|
||||
&pdfium,
|
||||
std::path::Path::new(&input),
|
||||
page,
|
||||
&mapped,
|
||||
std::path::Path::new(&output),
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Áp `ops` rồi ghi ra 1 file PDF tạm MỚI (duy nhất), trả về đường dẫn — dùng
|
||||
/// cho mô hình "materialize tức thì" ở UI: mỗi thao tác sửa tạo 1 bản làm việc
|
||||
/// mới để đọc lại object + render WYSIWYG, và để undo quay về bản trước.
|
||||
#[tauri::command]
|
||||
fn edit_apply_to_temp(input: String, page: u16, ops: Vec<EditOpDto>, password: Option<String>) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let mapped: Vec<ff_engine::EditOp> = ops.into_iter().map(edit_op_from_dto).collect::<Result<_, String>>()?;
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0);
|
||||
let out = std::env::temp_dir().join(format!("ff_edit_{}_{}.pdf", std::process::id(), nanos));
|
||||
ff_engine::apply_edits(
|
||||
&pdfium,
|
||||
std::path::Path::new(&input),
|
||||
page,
|
||||
&mapped,
|
||||
&out,
|
||||
password.as_deref(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(out.to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
/// Xem trước WYSIWYG: áp `ops` vào file tạm rồi render trang `page` ra PNG.
|
||||
#[tauri::command]
|
||||
fn edit_preview(input: String, page: u16, ops: Vec<EditOpDto>, width: u32, password: Option<String>) -> Result<String, String> {
|
||||
let pdfium = pdfium()?;
|
||||
let mapped: Vec<ff_engine::EditOp> = ops.into_iter().map(edit_op_from_dto).collect::<Result<_, String>>()?;
|
||||
let tmp = std::env::temp_dir().join(format!("ff_preview_edit_{}.pdf", std::process::id()));
|
||||
ff_engine::apply_edits(&pdfium, std::path::Path::new(&input), page, &mapped, &tmp, password.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = render_temp_page(&pdfium, &tmp, page, width);
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
result
|
||||
}
|
||||
|
||||
/// Hộp thoại chọn file ảnh (cho Thêm ảnh / Thay ảnh).
|
||||
#[tauri::command]
|
||||
fn pick_image(app: tauri::AppHandle) -> Option<String> {
|
||||
app.dialog()
|
||||
.file()
|
||||
.add_filter("Ảnh", &["png", "jpg", "jpeg", "bmp", "gif", "webp"])
|
||||
.blocking_pick_file()
|
||||
.map(|fp| fp.to_string())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
@@ -333,9 +842,26 @@ fn main() {
|
||||
page_text_layer,
|
||||
pick_pdf,
|
||||
pick_save_pdf,
|
||||
pick_dir,
|
||||
apply_annotations,
|
||||
default_pdf,
|
||||
initial_file
|
||||
initial_file,
|
||||
ensure_openable,
|
||||
organize_identity_plan,
|
||||
organize_apply,
|
||||
organize_extract,
|
||||
organize_merge,
|
||||
organize_split,
|
||||
organize_materialize,
|
||||
watermark_add,
|
||||
header_footer_add,
|
||||
preview_watermark,
|
||||
preview_header_footer,
|
||||
edit_list_objects,
|
||||
edit_apply,
|
||||
edit_apply_to_temp,
|
||||
edit_preview,
|
||||
pick_image
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("lỗi khi chạy ứng dụng Tauri");
|
||||
|
||||
+54
-5
@@ -30,11 +30,17 @@
|
||||
<button id="searchNext" title="Kết quả sau (Enter)">▶</button>
|
||||
<span class="sep"></span>
|
||||
<button id="copyPage" title="Copy text trang hiện tại">⧉ Copy</button>
|
||||
<span class="sep"></span>
|
||||
<button id="undoBtn" title="Hoàn tác (Ctrl+Z)" disabled>↶ Hoàn tác</button>
|
||||
<button id="redoBtn" title="Làm lại (Ctrl+Y)" disabled>↷ Làm lại</button>
|
||||
<span class="sep"></span>
|
||||
<button id="organizeModeBtn" title="Tổ chức trang">🗂 Tổ chức trang</button>
|
||||
<button id="editModeBtn" title="Sửa nội dung trang">✏️ Sửa nội dung</button>
|
||||
<div class="spacer"></div>
|
||||
<span id="status" class="status"></span>
|
||||
</header>
|
||||
|
||||
<div class="annobar">
|
||||
<div id="annobar" class="annobar">
|
||||
<span class="annolabel">Chú thích:</span>
|
||||
<button class="atool" data-tool="highlight" title="Tô sáng (kéo vùng)">🖍️ Tô sáng</button>
|
||||
<button class="atool" data-tool="underline" title="Gạch chân">U̲ Gạch chân</button>
|
||||
@@ -42,18 +48,48 @@
|
||||
<button class="atool" data-tool="square" title="Khung chữ nhật">▭ Khung</button>
|
||||
<button class="atool" data-tool="freetext" title="Hộp văn bản">🅣 Text box</button>
|
||||
<button class="atool" data-tool="note" title="Ghi chú dán">📌 Ghi chú</button>
|
||||
<button class="atool" data-tool="crop" title="Crop trang">✂️ Crop</button>
|
||||
<span class="sep"></span>
|
||||
<button id="annotColorBtn" class="atool" title="Màu chú thích"><span class="sw" id="annotSw"></span> Màu</button>
|
||||
<span class="sep"></span>
|
||||
<button id="undoBtn" title="Hoàn tác (Ctrl+Z)" disabled>↶ Hoàn tác</button>
|
||||
<button id="redoBtn" title="Làm lại (Ctrl+Y)" disabled>↷ Làm lại</button>
|
||||
<span class="spacer"></span>
|
||||
<span id="annotHint" class="status"></span>
|
||||
<button id="saveAnnots" disabled>💾 Lưu chú thích (<span id="annotCount">0</span>)</button>
|
||||
</div>
|
||||
|
||||
<div id="organizeBar" class="organizebar hidden">
|
||||
<span class="annolabel">Tổ chức trang:</span>
|
||||
<button id="orgInsert" title="Chèn trang">➕ Chèn</button>
|
||||
<button id="orgDelete" title="Xoá trang đã chọn (Delete)">🗑 Xoá</button>
|
||||
<button id="orgRotateL" title="Xoay trái">↺ Xoay trái</button>
|
||||
<button id="orgRotateR" title="Xoay phải">↻ Xoay phải</button>
|
||||
<button id="orgExtract" title="Trích trang đã chọn">📤 Trích</button>
|
||||
<button id="orgReplace" title="Thay trang đã chọn">🔁 Thay</button>
|
||||
<span class="sep"></span>
|
||||
<button id="orgMerge" title="Trộn nhiều file PDF">🔀 Trộn file</button>
|
||||
<button id="orgSplit" title="Tách thành nhiều file">✂ Tách file</button>
|
||||
<button id="orgWatermark" title="Thêm watermark">💧 Watermark</button>
|
||||
<button id="orgHeaderFooter" title="Thêm header/footer">📋 Header/Footer</button>
|
||||
<span class="spacer"></span>
|
||||
<span id="organizeHint" class="status"></span>
|
||||
<button id="orgSave" disabled>💾 Lưu thay đổi tổ chức trang</button>
|
||||
</div>
|
||||
|
||||
<div id="editBar" class="editbar hidden">
|
||||
<span class="annolabel">Sửa nội dung:</span>
|
||||
<button id="edAddText" title="Thêm chữ — bấm rồi click lên trang">🆎 Thêm chữ</button>
|
||||
<button id="edAddImage" title="Thêm ảnh — chọn ảnh rồi click lên trang">🖼 Thêm ảnh</button>
|
||||
<button id="edDelete" title="Xoá đối tượng đang chọn (Delete)" disabled>🗑 Xoá</button>
|
||||
<button id="edReplaceImage" title="Thay ảnh đang chọn" disabled>🔁 Thay ảnh</button>
|
||||
<span class="sep"></span>
|
||||
<label class="ed-prop">Cỡ chữ <input id="edFontSize" type="number" min="1" max="400" disabled></label>
|
||||
<button id="edColorBtn" class="atool" title="Màu chữ" disabled><span class="sw" id="edSw"></span> Màu</button>
|
||||
<span class="spacer"></span>
|
||||
<span id="editHint" class="status"></span>
|
||||
<button id="edSave" disabled>💾 Lưu thay đổi nội dung</button>
|
||||
</div>
|
||||
|
||||
<div class="body">
|
||||
<aside class="sidebar">
|
||||
<aside id="sidebar" class="sidebar">
|
||||
<div class="tabs">
|
||||
<button id="tabThumbs" class="tab active">Thumbnails</button>
|
||||
<button id="tabOutline" class="tab">Outline</button>
|
||||
@@ -67,6 +103,19 @@
|
||||
<main id="viewport" class="viewport">
|
||||
<div id="pages" class="pages"></div>
|
||||
</main>
|
||||
|
||||
<main id="organizeGrid" class="organize-grid hidden"></main>
|
||||
|
||||
<main id="editStage" class="edit-stage hidden">
|
||||
<div id="editPageWrap" class="edit-page-wrap">
|
||||
<img id="editImg" class="edit-img" alt="trang đang sửa"/>
|
||||
<div id="editOverlay" class="edit-overlay"></div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<div id="modalOverlay" class="modal-overlay hidden">
|
||||
<div id="modalBox" class="modal-box"></div>
|
||||
</div>
|
||||
|
||||
<script src="main.js"></script>
|
||||
|
||||
+1168
-19
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
:root { color-scheme: dark; --accent: #4d8bff; }
|
||||
* { box-sizing: border-box; }
|
||||
.hidden { display: none !important; }
|
||||
html, body { height: 100%; margin: 0; }
|
||||
body {
|
||||
font-family: "Segoe UI", system-ui, sans-serif;
|
||||
@@ -12,6 +13,7 @@ body {
|
||||
/* Toolbar */
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 12px;
|
||||
@@ -47,6 +49,7 @@ button {
|
||||
button:hover { background: #595f63; }
|
||||
button:disabled { opacity: .4; cursor: default; }
|
||||
#copyPage { background: var(--accent); }
|
||||
#organizeModeBtn.active { background: var(--accent); }
|
||||
select.zoom {
|
||||
background: #1f2224; color: #eee; border: 1px solid #4a4f52;
|
||||
border-radius: 6px; padding: 4px 4px; font-size: 13px; min-width: 64px;
|
||||
@@ -63,6 +66,7 @@ input[type="search"] {
|
||||
/* Annotation toolbar */
|
||||
.annobar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 5px 12px;
|
||||
@@ -266,3 +270,143 @@ body.tool-active .annotlayer * { pointer-events: none; }
|
||||
outline: 1px solid rgba(255, 184, 0, .9);
|
||||
}
|
||||
.hl.current { background: rgba(255, 138, 0, .55); }
|
||||
|
||||
/* ===== Phase 3: Tổ chức trang ===== */
|
||||
.organizebar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 5px 12px;
|
||||
background: #2b2f31;
|
||||
border-bottom: 1px solid #1c1f21;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
#orgSave { background: #2e7d32; }
|
||||
#orgSave:disabled { background: #555; }
|
||||
|
||||
.organize-grid {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-content: flex-start;
|
||||
gap: 18px;
|
||||
padding: 24px;
|
||||
background: #3a3e41;
|
||||
}
|
||||
.org-card {
|
||||
width: 160px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
border: 2px solid transparent;
|
||||
border-radius: 6px;
|
||||
padding: 6px;
|
||||
background: #2b2f31;
|
||||
}
|
||||
.org-card.selected { border-color: var(--accent); background: #2f3a4d; }
|
||||
.org-card.drag-over { outline: 3px dashed var(--accent); }
|
||||
.org-card .org-thumb-wrap {
|
||||
position: relative;
|
||||
background: #fff;
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,.5);
|
||||
overflow: hidden;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
min-height: 60px;
|
||||
}
|
||||
.org-card img { display: block; width: 100%; transition: transform .15s; }
|
||||
.org-card .org-n { font-size: 12px; color: #ccc; margin-top: 4px; }
|
||||
.org-card .org-crop-mark {
|
||||
position: absolute; inset: 10%; border: 1.5px dashed #e53935; pointer-events: none;
|
||||
}
|
||||
|
||||
/* Modal dialog dùng chung cho Insert/Extract/Replace/Crop/Merge/Split/Watermark/Header-Footer */
|
||||
.modal-overlay {
|
||||
position: fixed; inset: 0; background: rgba(0,0,0,.55);
|
||||
display: flex; align-items: center; justify-content: center; z-index: 100;
|
||||
}
|
||||
.modal-box {
|
||||
background: #2b2f31; border-radius: 8px; padding: 16px 20px;
|
||||
min-width: 340px; max-width: 560px; max-height: 84vh; overflow: auto;
|
||||
box-shadow: 0 10px 40px rgba(0,0,0,.6); color: #eee;
|
||||
}
|
||||
.modal-box h3 { margin: 0 0 12px; font-size: 15px; color: #fff; }
|
||||
.modal-box label { display: block; font-size: 12px; color: #9aa0a6; margin: 10px 0 3px; }
|
||||
.modal-box input[type="text"], .modal-box input[type="number"], .modal-box select, .modal-box textarea {
|
||||
width: 100%; background: #1f2224; color: #eee; border: 1px solid #4a4f52;
|
||||
border-radius: 6px; padding: 6px 8px; font-size: 13px; box-sizing: border-box;
|
||||
}
|
||||
.modal-box .row { display: flex; gap: 8px; }
|
||||
.modal-box .row > * { flex: 1; }
|
||||
.modal-box .radiorow { display: flex; gap: 14px; align-items: center; font-size: 13px; margin: 4px 0; }
|
||||
.modal-box .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
|
||||
.modal-box .foot button.primary { background: var(--accent); }
|
||||
.modal-box .anchor9 { display: grid; grid-template-columns: repeat(3, 32px); gap: 4px; }
|
||||
.modal-box .anchor9 button { width: 32px; height: 28px; padding: 0; background: #3a3f42; }
|
||||
.modal-box .anchor9 button.cur { background: var(--accent); }
|
||||
.modal-box .err { color: #ef5350; font-size: 12px; margin-top: 8px; }
|
||||
|
||||
/* ===== Phase 4: Sửa nội dung (Edit) ===== */
|
||||
#editModeBtn.active { background: var(--accent); }
|
||||
.editbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 5px 12px;
|
||||
background: #2b2f31;
|
||||
border-bottom: 1px solid #1c1f21;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
.editbar .ed-prop { display: flex; align-items: center; gap: 4px; font-size: 13px; }
|
||||
.editbar .ed-prop input { width: 56px; }
|
||||
.editbar button.armed { background: var(--accent); }
|
||||
#edSave { background: #2e7d32; }
|
||||
#edSave:disabled { background: #555; }
|
||||
|
||||
.edit-stage {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
background: #444;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: flex-start;
|
||||
padding: 20px;
|
||||
user-select: none;
|
||||
}
|
||||
.edit-inline { user-select: text; }
|
||||
.edit-page-wrap { position: relative; box-shadow: 0 2px 12px rgba(0,0,0,.5); background: #fff; }
|
||||
.edit-img { display: block; }
|
||||
.edit-overlay { position: absolute; inset: 0; }
|
||||
.edit-overlay.armed { cursor: crosshair; }
|
||||
|
||||
.edit-box {
|
||||
position: absolute;
|
||||
border: 1px solid rgba(77,139,255,.5);
|
||||
box-sizing: border-box;
|
||||
cursor: move;
|
||||
}
|
||||
.edit-box:hover { border-color: var(--accent); background: rgba(77,139,255,.08); }
|
||||
.edit-box.kind-text { border-style: dashed; }
|
||||
.edit-box.selected { border: 2px solid var(--accent); background: rgba(77,139,255,.12); }
|
||||
.edit-box .ed-handle {
|
||||
position: absolute;
|
||||
width: 10px; height: 10px;
|
||||
right: -6px; bottom: -6px;
|
||||
background: var(--accent);
|
||||
border: 1px solid #fff;
|
||||
cursor: nwse-resize;
|
||||
}
|
||||
.edit-inline {
|
||||
position: absolute;
|
||||
z-index: 5;
|
||||
border: 2px solid var(--accent);
|
||||
background: #fff;
|
||||
color: #000;
|
||||
padding: 1px 2px;
|
||||
outline: none;
|
||||
font-family: inherit;
|
||||
overflow: hidden;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user