Skip to content

Commit

Permalink
Feature image occlusion (#2367)
Browse files Browse the repository at this point in the history
* add note types with occlusions and image fields

* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer

* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist

* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari

* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs

* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools

* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)

* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
     - for adding note using path to image
     - for editing note using note id

* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes

* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor

* implement generates notes and save notes
implemention to show toast components for messages

* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support

* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend

* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation

* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors

* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly

* add and get notetype else return errors

* fix: not showing occlusion

* Use a oneof for ImageClozeNoteResponse

Makes it clearer that only one of them can be returned

* Don't crash if image filename not provided

The second unwrap should be ok, as the input is utf8

* Refactor get_image_cloze_note

- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields

* Fix crash if image file unreadable

+ Use our read_file helper for better error context

* Add metadata() helper

* Fix crash if file metadata can't be read

* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes

* update color in reviewer and ftl strings

* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent

* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also

* fix border width and color in reviewer canvas
- rename variable

* refactor cloze div generate and remove angle

* fix origin when drawn outside of canvas from right

* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only

* fix lint errors & update image size in editor canvas based on height and width

* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges

* implemented undo redo with canvas state

- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo

* remove background image from canvas and used css to put image tag below canvas editor

- set image width and height after adding image

* fix for polygon points, add br in cloze strings, & toogle masks button

- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)

* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border

* fix undo after undo happen, use transparent color in draw mode
  • Loading branch information
krmanik committed Mar 29, 2023
1 parent c691c9b commit 2bf134d
Show file tree
Hide file tree
Showing 60 changed files with 4,750 additions and 7 deletions.
12 changes: 12 additions & 0 deletions build/configure/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ fn build_and_check_pages(build: &mut Build) -> Result<()> {
":sass"
],
)?;
build_page(
"image-occlusion",
true,
inputs![
//
":ts:lib",
":ts:components",
":ts:sveltelib",
":ts:tag-editor",
":sass"
],
)?;

Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions ftl/core/importing.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ importing-tag-updated-notes = Tag updated notes
importing-file = File
importing-match-scope = Match scope
importing-notetype-and-deck = Notetype and deck
importing-cards-added =
{ $count ->
[one] { $count } card added.
*[other] { $count } cards added.
}
## NO NEED TO TRANSLATE. This text is no longer used by Anki, and will be removed in the future.

Expand Down
15 changes: 15 additions & 0 deletions ftl/core/notetypes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ notetypes-note-types = Note Types
notetypes-options = Options
notetypes-please-add-another-note-type-first = Please add another note type first.
notetypes-type = Type
## Image Occlusion

notetypes-image = Image
notetypes-occlusion = Occlusion
notetypes-occlusion-mask = Mask
notetypes-occlusion-note = Note
notetypes-comments-field = Comments
notetypes-toggle-masks = Toggle Masks
notetypes-image-occlusion-name = Image Occlusion
notetypes-hide-all-guess-one = Hide All, Guess One
notetypes-hide-one-guess-one = Hide One, Guess One
notetypes-error-generating-cloze = An error occurred when generating an image occlusion note
notetypes-error-getting-imagecloze = An error occurred while fetching an image occlusion note
notetypes-error-loading-image-occlusion = Error loading image occlusion. Is your Anki version up to date?
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"codemirror": "^5.63.1",
"css-browser-selector": "^0.6.5",
"d3": "^7.0.0",
"fabric": "^5.3.0",
"fuse.js": "^6.6.2",
"gemoji": "^7.1.0",
"intl-pluralrules": "^1.2.2",
Expand All @@ -82,6 +83,7 @@
"lodash-es": "^4.17.21",
"marked": "^4.0.0",
"mathjax": "^3.1.2",
"panzoom": "^9.4.3",
"protobufjs": "^7"
},
"resolutions": {
Expand Down
1 change: 1 addition & 0 deletions proto/anki/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum ServiceIndex {
SERVICE_INDEX_LINKS = 15;
SERVICE_INDEX_IMPORT_EXPORT = 16;
SERVICE_INDEX_ANKIDROID = 17;
SERVICE_INDEX_IMAGE_OCCLUSION = 18;
}

message BackendInit {
Expand Down
67 changes: 67 additions & 0 deletions proto/anki/image_occlusion.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

syntax = "proto3";

option java_multiple_files = true;

package anki.image_occlusion;

import "anki/cards.proto";
import "anki/collection.proto";
import "anki/notes.proto";
import "anki/generic.proto";

service ImageOcclusionService {
rpc GetImageForOcclusion(GetImageForOcclusionRequest) returns (ImageData);
rpc AddImageOcclusionNote(AddImageOcclusionNoteRequest)
returns (collection.OpChanges);
rpc GetImageClozeNote(GetImageOcclusionNoteRequest)
returns (ImageClozeNoteResponse);
rpc UpdateImageOcclusionNote(UpdateImageOcclusionNoteRequest)
returns (collection.OpChanges);
}

message GetImageForOcclusionRequest {
string path = 1;
}

message ImageData {
bytes data = 1;
string name = 2;
}

message AddImageOcclusionNoteRequest {
string image_path = 1;
string occlusions = 2;
string header = 3;
string back_extra = 4;
repeated string tags = 5;
}

message ImageClozeNote {
bytes image_data = 1;
string occlusions = 2;
string header = 3;
string back_extra = 4;
repeated string tags = 5;
}

message GetImageOcclusionNoteRequest {
int64 note_id = 1;
}

message UpdateImageOcclusionNoteRequest {
int64 note_id = 1;
string occlusions = 2;
string header = 3;
string back_extra = 4;
repeated string tags = 5;
}

message ImageClozeNoteResponse {
oneof value {
ImageClozeNote note = 1;
string error = 2;
}
}
1 change: 1 addition & 0 deletions proto/anki/notetypes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ message StockNotetype {
BASIC_OPTIONAL_REVERSED = 2;
BASIC_TYPING = 3;
CLOZE = 4;
IMAGE_OCCLUSION = 5;
}

Kind kind = 1;
Expand Down
44 changes: 44 additions & 0 deletions pylib/anki/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
collection_pb2,
config_pb2,
generic_pb2,
image_occlusion_pb2,
import_export_pb2,
links_pb2,
search_pb2,
Expand Down Expand Up @@ -40,6 +41,9 @@
DupeResolution = CsvMetadata.DupeResolution
Delimiter = import_export_pb2.CsvMetadata.Delimiter
TtsVoice = card_rendering_pb2.AllTtsVoicesResponse.TtsVoice
ImageData = image_occlusion_pb2.ImageData
AddImageOcclusionNoteRequest = image_occlusion_pb2.AddImageOcclusionNoteRequest
ImageClozeNoteResponse = image_occlusion_pb2.ImageClozeNoteResponse

import copy
import os
Expand Down Expand Up @@ -456,6 +460,46 @@ def import_json_file(self, path: str) -> ImportLogWithChanges:
def import_json_string(self, json: str) -> ImportLogWithChanges:
return self._backend.import_json_string(json)

# Image Occlusion
##########################################################################
def get_image_for_occlusion(self, path: str | None) -> ImageData:
return self._backend.get_image_for_occlusion(path=path)

def add_image_occlusion_note(
self,
image_path: str | None,
occlusions: str | None,
header: str | None,
back_extra: str | None,
tags: list[str] | None,
) -> OpChanges:
return self._backend.add_image_occlusion_note(
image_path=image_path,
occlusions=occlusions,
header=header,
back_extra=back_extra,
tags=tags,
)

def get_image_cloze_note(self, note_id: int | None) -> ImageClozeNoteResponse:
return self._backend.get_image_cloze_note(note_id=note_id)

def update_image_occlusion_note(
self,
note_id: int | None,
occlusions: str | None,
header: str | None,
back_extra: str | None,
tags: list[str] | None,
) -> OpChanges:
return self._backend.update_image_occlusion_note(
note_id=note_id,
occlusions=occlusions,
header=header,
back_extra=back_extra,
tags=tags,
)

# Object helpers
##########################################################################

Expand Down
3 changes: 3 additions & 0 deletions pylib/tools/genbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import anki.deckconfig_pb2
import anki.decks_pb2
import anki.i18n_pb2
import anki.image_occlusion_pb2
import anki.import_export_pb2
import anki.links_pb2
import anki.media_pb2
Expand Down Expand Up @@ -187,6 +188,7 @@ def render_service(
MEDIA=anki.media_pb2,
LINKS=anki.links_pb2,
IMPORT_EXPORT=anki.import_export_pb2,
IMAGE_OCCLUSION=anki.image_occlusion_pb2,
)

for service in anki.backend_pb2.ServiceIndex.DESCRIPTOR.values:
Expand Down Expand Up @@ -238,6 +240,7 @@ def render_service(
import anki.tags_pb2
import anki.media_pb2
import anki.import_export_pb2
import anki.image_occlusion_pb2
class RustBackendGenerated:
def _run_command(self, service: int, method: int, input: Any) -> bytes:
Expand Down
5 changes: 5 additions & 0 deletions qt/aqt/mediasrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ def handle_on_main() -> None:
"set_graph_preferences",
# TagsService
"complete_tag",
# ImageOcclusionService
"get_image_for_occlusion",
"add_image_occlusion_note",
"get_image_cloze_note",
"update_image_occlusion_note",
]


Expand Down
55 changes: 55 additions & 0 deletions rslib/src/backend/image_occlusion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

use super::Backend;
pub(super) use crate::pb::image_occlusion::imageocclusion_service::Service as ImageOcclusionService;
use crate::pb::{self as pb,};
use crate::prelude::*;

impl ImageOcclusionService for Backend {
fn get_image_for_occlusion(
&self,
input: pb::image_occlusion::GetImageForOcclusionRequest,
) -> Result<pb::image_occlusion::ImageData> {
self.with_col(|col| col.get_image_for_occlusion(&input.path))
}

fn add_image_occlusion_note(
&self,
input: pb::image_occlusion::AddImageOcclusionNoteRequest,
) -> Result<pb::collection::OpChanges> {
self.with_col(|col| {
col.add_image_occlusion_note(
&input.image_path,
&input.occlusions,
&input.header,
&input.back_extra,
input.tags,
)
})
.map(Into::into)
}

fn get_image_cloze_note(
&self,
input: pb::image_occlusion::GetImageOcclusionNoteRequest,
) -> Result<pb::image_occlusion::ImageClozeNoteResponse> {
self.with_col(|col| col.get_image_cloze_note(input.note_id.into()))
}

fn update_image_occlusion_note(
&self,
input: pb::image_occlusion::UpdateImageOcclusionNoteRequest,
) -> Result<pb::collection::OpChanges> {
self.with_col(|col| {
col.update_image_occlusion_note(
input.note_id.into(),
&input.occlusions,
&input.header,
&input.back_extra,
input.tags,
)
})
.map(Into::into)
}
}
5 changes: 5 additions & 0 deletions rslib/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod decks;
mod error;
mod generic;
mod i18n;
mod image_occlusion;
mod import_export;
mod links;
mod media;
Expand Down Expand Up @@ -48,6 +49,7 @@ use self::config::ConfigService;
use self::deckconfig::DeckConfigService;
use self::decks::DecksService;
use self::i18n::I18nService;
use self::image_occlusion::ImageOcclusionService;
use self::import_export::ImportExportService;
use self::links::LinksService;
use self::media::MediaService;
Expand Down Expand Up @@ -142,6 +144,9 @@ impl Backend {
ServiceIndex::Collection => CollectionService::run_method(self, method, input),
ServiceIndex::Cards => CardsService::run_method(self, method, input),
ServiceIndex::ImportExport => ImportExportService::run_method(self, method, input),
ServiceIndex::ImageOcclusion => {
ImageOcclusionService::run_method(self, method, input)
}
})
.map_err(|err| {
let backend_err = err.into_protobuf(&self.tr);
Expand Down
Loading

0 comments on commit 2bf134d

Please sign in to comment.