Skip to content

Commit

Permalink
fundation
Browse files Browse the repository at this point in the history
  • Loading branch information
panaC committed Aug 15, 2023
1 parent ba555ad commit 13f3d29
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/common/redux/states/annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ==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 { IHighlightDefinition } from "@r2-navigator-js/electron/common/highlight";
import { TPQueueState } from "readium-desktop/utils/redux-reducers/pqueue.reducer";

export type TAnnotationState = TPQueueState<number, IAnnotationState>;

export interface IAnnotationState {
uuid: string;
name: string; // like bookmark name
comment: string; // describe annotation mark
hash: string; // sha256 ( base64 ( {href, def} ))
href: string; // from IHighlightHandlerState
def: IHighlightDefinition; // from IHighlightHandlerState
}

export type IAnnotationStateWithoutUUID = Partial<Pick<IAnnotationState, "uuid">> & Pick<IAnnotationState, "name" | "comment" | "def" | "hash" | "href">;
2 changes: 2 additions & 0 deletions src/common/redux/states/renderer/readerRootState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IHighlight } from "@r2-navigator-js/electron/common/highlight";
import { LocatorExtended } from "@r2-navigator-js/electron/renderer";

import { TBookmarkState } from "../bookmark";
import { TAnnotationState } from "../annotation";

export interface IReaderRootState extends ICommonRootState {
reader: IReaderStateReader;
Expand All @@ -30,6 +31,7 @@ export interface IReaderStateReader {
info: ReaderInfo;
locator: LocatorExtended;
bookmark: TBookmarkState;
annotation: TAnnotationState;
highlight: {
handler: TMapState<string, IHighlightHandlerState>;
mounter: TMapState<string, IHighlight>;
Expand Down
30 changes: 24 additions & 6 deletions src/renderer/reader/components/ReaderMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ import { TKeyboardEventButton, TMouseEventOnButton } from "readium-desktop/typin
import { TDispatch } from "readium-desktop/typings/redux";
import { Unsubscribe } from "redux";

import { LocatorExtended } from "@r2-navigator-js/electron/renderer/index";
import { handleLinkLocator, LocatorExtended } from "@r2-navigator-js/electron/renderer/index";

Check failure on line 27 in src/renderer/reader/components/ReaderMenu.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest)

'handleLinkLocator' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 27 in src/renderer/reader/components/ReaderMenu.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'handleLinkLocator' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 27 in src/renderer/reader/components/ReaderMenu.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04)

'handleLinkLocator' is defined but never used. Allowed unused vars must match /^_/u
import { Link } from "@r2-shared-js/models/publication-link";

import { ILink, TToc } from "../pdf/common/pdfReader.type";
import { readerLocalActionBookmarks } from "../redux/actions";
import { readerLocalActionBookmarks, readerLocalActionAnnotations } from "../redux/actions";
import { IReaderMenuProps } from "./options-values";
import ReaderMenuSearch from "./ReaderMenuSearch";
import SideMenu from "./sideMenu/SideMenu";
import { SectionData } from "./sideMenu/sideMenuData";
import UpdateBookmarkForm from "./UpdateBookmarkForm";
import { IAnnotationState } from "readium-desktop/common/redux/states/annotation";
import { IRangeInfo } from "r2-navigator-js/dist/es8-es2017/src/electron/common/selection";

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IBaseProps extends TranslatorProps, IReaderMenuProps {
Expand Down Expand Up @@ -149,11 +151,11 @@ export class ReaderMenu extends React.Component<IProps, IState> {
content: this.createBookmarkList(),
disabled: !bookmarks || bookmarks.length === 0,
},
/*{
{
title: __("reader.marks.annotations"),
content: <></>,
disabled: true,
},*/
disabled: false,
},
{
title: __("reader.marks.search"),
content: this.props.searchEnable
Expand Down Expand Up @@ -821,6 +823,16 @@ export class ReaderMenu extends React.Component<IProps, IState> {
e.preventDefault();
this.props.goToLocator(locator, closeNavPanel);
}

// from src/renderer/reader/redux/sagas/search.ts
private createLocatorLink(href: string, rangeInfo: IRangeInfo): Locator {
return {
href,
locations: {
cssSelector: rangeInfo.startContainerElementCssSelector,
},
};
}
}

const mapStateToProps = (state: IReaderRootState, _props: IBaseProps) => {
Expand All @@ -834,20 +846,26 @@ const mapStateToProps = (state: IReaderRootState, _props: IBaseProps) => {
pubId: state.reader.info.publicationIdentifier,
searchEnable: state.search.enable,
bookmarks: state.reader.bookmark.map(([, v]) => v),
highlights: state.reader.highlight.handler.map(([, v]) => v),
// isDivina,
};
};

const mapDispatchToProps = (dispatch: TDispatch) => {


return {
setBookmark: (bookmark: IBookmarkState) => {
dispatch(readerLocalActionBookmarks.push.build(bookmark));
},
deleteBookmark: (bookmark: IBookmarkState) => {
dispatch(readerLocalActionBookmarks.pop.build(bookmark));
},
setAnnotation: (annotation: IAnnotationState) => {
dispatch(readerLocalActionAnnotations.push.build(annotation));
},
deleteAnnotation: (annotation: IAnnotationState) => {
dispatch(readerLocalActionAnnotations.pop.build(annotation));
},
};
};

Expand Down
16 changes: 16 additions & 0 deletions src/renderer/reader/redux/actions/annotations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ==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 pop from "./pop";
import * as push from "./push";
import * as update from "./update";

export {
push,
pop,
update,
};
26 changes: 26 additions & 0 deletions src/renderer/reader/redux/actions/annotations/pop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==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";
import { IAnnotationState } from "readium-desktop/common/redux/states/annotation";

export const ID = "READER_ANNOTATIONS_POP";

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IPayload extends IAnnotationState {
}

export function build(param: IAnnotationState):
Action<typeof ID, IPayload> {

return {
type: ID,
payload: param,
};
}
build.toString = () => ID; // Redux StringableActionCreator
export type TAction = ReturnType<typeof build>;
29 changes: 29 additions & 0 deletions src/renderer/reader/redux/actions/annotations/push.ts
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";
import { IAnnotationStateWithoutUUID, IAnnotationState } from "readium-desktop/common/redux/states/annotation";
import { v4 as uuidv4 } from "uuid";

export const ID = "READER_ANNOTATIONS_PUSH";

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IPayload extends IAnnotationState {
}

export function build(param: IAnnotationStateWithoutUUID):
Action<typeof ID, IPayload> {

param.uuid = param.uuid || uuidv4();

return {
type: ID,
payload: param as IAnnotationState,
};
}
build.toString = () => ID; // Redux StringableActionCreator
export type TAction = ReturnType<typeof build>;
26 changes: 26 additions & 0 deletions src/renderer/reader/redux/actions/annotations/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==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";
import { IAnnotationState } from "readium-desktop/common/redux/states/annotation";

export const ID = "READER_ANNOTATIONS_UPDATE";

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IPayload extends IAnnotationState {
}

export function build(param: IAnnotationState):
Action<typeof ID, IPayload> {

return {
type: ID,
payload: param,
};
}
build.toString = () => ID; // Redux StringableActionCreator
export type TAction = ReturnType<typeof build>;
2 changes: 2 additions & 0 deletions src/renderer/reader/redux/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==LICENSE-END==

import * as readerLocalActionBookmarks from "./bookmarks";
import * as readerLocalActionAnnotations from "./annotations";
import * as readerLocalActionDivina from "./divina";
import * as readerLocalActionHighlights from "./highlights";
import * as readerLocalActionLocatorHrefChanged from "./locatorHrefChanged";
Expand All @@ -22,5 +23,6 @@ export {
readerLocalActionPicker,
readerLocalActionSearch,
readerLocalActionBookmarks,
readerLocalActionAnnotations,
readerLocalActionDivina,
};
5 changes: 5 additions & 0 deletions src/renderer/reader/redux/middleware/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export const reduxPersistMiddleware: Middleware
readerState.bookmark = nextState.reader.bookmark;
dispatchFlag = true;
}
if (!ramda.equals(prevState.reader.annotation, nextState.reader.annotation)) {

readerState.annotation = nextState.reader.annotation;
dispatchFlag = true;
}
if (!ramda.equals(prevState.reader.divina, nextState.reader.divina)) {

readerState.divina = nextState.reader.divina;
Expand Down
34 changes: 33 additions & 1 deletion src/renderer/reader/redux/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { combineReducers } from "redux";

import { IHighlight } from "@r2-navigator-js/electron/common/highlight";

import { readerLocalActionBookmarks, readerLocalActionHighlights } from "../actions";
import { readerLocalActionBookmarks, readerLocalActionAnnotations, readerLocalActionHighlights } from "../actions";
import { IHighlightHandlerState } from "../state/highlight";
import { readerInfoReducer } from "./info";
import { pickerReducer } from "./picker";
Expand All @@ -30,6 +30,7 @@ import { IBookmarkState } from "readium-desktop/common/redux/states/bookmark";
import { priorityQueueReducer } from "readium-desktop/utils/redux-reducers/pqueue.reducer";
import { winModeReducer } from "readium-desktop/common/redux/reducers/winModeReducer";
import { readerDivinaReducer } from "./divina";
import { IAnnotationState } from "readium-desktop/common/redux/states/annotation";

export const rootReducer = () => {
return combineReducers<IReaderRootState>({
Expand Down Expand Up @@ -70,6 +71,37 @@ export const rootReducer = () => {
},
},
),
annotation: priorityQueueReducer
<
readerLocalActionAnnotations.push.TAction,
readerLocalActionAnnotations.pop.TAction,
number,
IAnnotationState,
string,
readerLocalActionAnnotations.update.TAction
>(
{
push: {
type: readerLocalActionAnnotations.push.ID,
selector: (action) =>
[(new Date()).getTime(), action.payload],
},
pop: {
type: readerLocalActionAnnotations.pop.ID,
selector: (action) =>
[undefined, action.payload],
},
sortFct: (a, b) => b[0] - a[0],
update: {
type: readerLocalActionAnnotations.update.ID,
selector: (action, queue) =>
[
queue.reduce<number>((pv, [k, v]) => v.uuid === action.payload.uuid ? k : pv, undefined),
action.payload,
],
},
},
),
highlight: combineReducers({
handler: mapReducer
<
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/reader/redux/sagas/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import { nanoid } from "nanoid";
import { takeSpawnEvery } from "readium-desktop/common/redux/sagas/takeSpawnEvery";
import { SagaIterator } from "redux-saga";
import { all, put } from "redux-saga/effects";

import { readerLocalActionHighlights, readerLocalActionSetLocator } from "../actions";
import { IHighlightHandlerState } from "../state/highlight";
import { all, put as putTyped } from "typed-redux-saga";

//
// WIP
Expand All @@ -25,6 +25,10 @@ function* selectionInfoWatcher(action: readerLocalActionSetLocator.TAction): Sag
locator: { href },
} = action.payload;

// 1. Check if annotation mode is enabled


// 2. Check if it is a user selection and a new selection
if (
selectionInfo
&& selectionIsNew
Expand All @@ -43,7 +47,7 @@ function* selectionInfoWatcher(action: readerLocalActionSetLocator.TAction): Sag
selectionInfo,
},
};
yield put(readerLocalActionHighlights.handler.push.build(def));
yield* putTyped(readerLocalActionHighlights.handler.push.build(def));
}
}

Expand Down

0 comments on commit 13f3d29

Please sign in to comment.