-
Notifications
You must be signed in to change notification settings - Fork 158
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
8 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/common/redux/actions/annotation/exportW3CAnnotationSet.ts
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,29 @@ | ||
// ==LICENSE-BEGIN== | ||
// Copyright 2017 European Digital Reading Lab. All rights reserved. | ||
// Licensed to the Readium Foundation under one or more contributor license agreements. | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the LICENSE file exposed on Github (readium) in the project repository. | ||
// ==LICENSE-END== | ||
|
||
import { Action } from "readium-desktop/common/models/redux"; | ||
|
||
export const ID = "ANNOTATION_EXPORT_W3CANNOTATIONSET"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface Payload { | ||
publicationIdentifier: string; | ||
filePath: string, | ||
} | ||
|
||
export function build(publicationIdentifier: string, filePath: string): Action<typeof ID, Payload> { | ||
|
||
return { | ||
type: ID, | ||
payload: { | ||
publicationIdentifier, | ||
filePath, | ||
}, | ||
}; | ||
} | ||
build.toString = () => ID; // Redux StringableActionCreator | ||
export type TAction = ReturnType<typeof build>; |
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,12 @@ | ||
// ==LICENSE-BEGIN== | ||
// Copyright 2017 European Digital Reading Lab. All rights reserved. | ||
// Licensed to the Readium Foundation under one or more contributor license agreements. | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the LICENSE file exposed on Github (readium) in the project repository. | ||
// ==LICENSE-END== | ||
|
||
import * as exportW3CAnnotationSetFromAnnotations from "./exportW3CAnnotationSet"; | ||
|
||
export { | ||
exportW3CAnnotationSetFromAnnotations, | ||
}; |
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,57 @@ | ||
// ==LICENSE-BEGIN== | ||
// Copyright 2017 European Digital Reading Lab. All rights reserved. | ||
// Licensed to the Readium Foundation under one or more contributor license agreements. | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the LICENSE file exposed on Github (readium) in the project repository. | ||
// ==LICENSE-END== | ||
|
||
import * as debug_ from "debug"; | ||
// import { LocaleConfigIdentifier, LocaleConfigValueType } from "readium-desktop/common/config"; | ||
import { annotationActions } from "readium-desktop/common/redux/actions"; | ||
import { takeSpawnLeading } from "readium-desktop/common/redux/sagas/takeSpawnLeading"; | ||
import { error } from "readium-desktop/main/tools/error"; | ||
// eslint-disable-next-line local-rules/typed-redux-saga-use-typed-effects | ||
import { call as callTyped, select as selectTyped } from "typed-redux-saga/macro"; | ||
import { getPublication } from "./api/publication/getPublication"; | ||
import { convertAnnotationListToW3CAnnotationModelSet, convertPublicationToAnnotationStateAbout } from "readium-desktop/main/w3c/annotation/converter"; | ||
import { RootState } from "../states"; | ||
import { IAnnotationState } from "readium-desktop/common/redux/states/annotation"; | ||
import { saveW3CAnnotationModelSetFromFilePath } from "readium-desktop/main/w3c/annotation/fs"; | ||
|
||
// Logger | ||
const filename_ = "readium-desktop:main:saga:annotation"; | ||
const debug = debug_(filename_); | ||
debug("_"); | ||
|
||
function* exportW3CAnnotation(action: annotationActions.exportW3CAnnotationSetFromAnnotations.TAction) { | ||
|
||
const { publicationIdentifier, filePath } = action.payload; | ||
|
||
const pub = yield* callTyped(getPublication, publicationIdentifier); | ||
|
||
let annotations: IAnnotationState[] = []; | ||
|
||
const sessionReader = yield* selectTyped((state: RootState) => state.win.session.reader); | ||
if (Object.keys(sessionReader).find((v) => v === publicationIdentifier)) { | ||
annotations = (sessionReader[publicationIdentifier]?.reduxState?.annotation || []).map(([,v]) => v); | ||
} else { | ||
const sessionRegistry = yield* selectTyped((state: RootState) => state.win.registry.reader); | ||
if (Object.keys(sessionRegistry).find((v) => v === publicationIdentifier)) { | ||
annotations = (sessionRegistry[publicationIdentifier]?.reduxState?.annotation || []).map(([, v]) => v); | ||
} | ||
} | ||
|
||
const publicationMetadata = yield* callTyped(convertPublicationToAnnotationStateAbout, pub, publicationIdentifier); | ||
const W3CAnnotationSet = yield* callTyped(convertAnnotationListToW3CAnnotationModelSet, annotations, publicationMetadata); | ||
|
||
yield* callTyped(saveW3CAnnotationModelSetFromFilePath, filePath, W3CAnnotationSet); | ||
} | ||
|
||
export function saga() { | ||
|
||
return takeSpawnLeading( | ||
annotationActions.exportW3CAnnotationSetFromAnnotations.ID, | ||
exportW3CAnnotation, | ||
(e) => error(filename_, e), | ||
); | ||
} |
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
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,20 @@ | ||
// ==LICENSE-BEGIN== | ||
// Copyright 2017 European Digital Reading Lab. All rights reserved. | ||
// Licensed to the Readium Foundation under one or more contributor license agreements. | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the LICENSE file exposed on Github (readium) in the project repository. | ||
// ==LICENSE-END== | ||
|
||
import { writeFileSync } from "fs"; | ||
import { IW3CAnnotationModelSet } from "./annotationModel.type"; | ||
|
||
export function saveW3CAnnotationModelSetFromFilePath(filePath: string, data: IW3CAnnotationModelSet) { | ||
try { | ||
const jsonData = JSON.stringify(data, null, 2); | ||
writeFileSync(filePath, jsonData, "utf-8"); | ||
|
||
console.log(`Data successfully written to ${filePath}`); | ||
} catch (error) { | ||
console.error(`Error writing data to ${filePath}: ${error}`); | ||
} | ||
} |
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