Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previously existing bookmark highlight range adjustment #1079

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions app/bibleview-js/src/components/BookmarkButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<div :class="{highlighted: !bookmark.wholeVerse}" class="bookmark-button" @click="toggleWholeVerse">
<FontAwesomeIcon icon="text-width"/>
</div>
<div v-if="!bookmark.wholeVerse && documentId" class="bookmark-button" @click="adjustRange">
<FontAwesomeIcon icon="text-width"/>
</div>
<template v-if="showStudyPadButtons">
<div
v-for="label of labels.filter(l => l.isRealLabel)"
Expand Down Expand Up @@ -59,19 +62,22 @@
<script>
import {useCommon} from "@/composables";
import {computed, ref} from "@vue/reactivity";
import {inject} from "@vue/runtime-core";
import {inject, nextTick} from "@vue/runtime-core";
import AreYouSure from "@/components/modals/AreYouSure";
import {FontAwesomeIcon} from "@fortawesome/vue-fontawesome";
import {findNodeAtOffsetWithNullOffset} from "@/utils";
import {emit, Events} from "@/eventbus";

export default {
name: "BookmarkButtons",
props: {
bookmark: {type: Object, required: true},
showStudyPadButtons: {type: Boolean, default: false},
documentId: {type: String, default: null},
},
emits: ["close-bookmark", "info-clicked"],
components: {AreYouSure, FontAwesomeIcon},
setup(props, {emit}) {
setup(props, {emit: $emit}) {
const areYouSure = ref(null);

const bookmark = computed(() => props.bookmark);
Expand Down Expand Up @@ -102,14 +108,41 @@ export default {

async function removeBookmark() {
if(await areYouSure.value.areYouSure()) {
emit("close-bookmark");
$emit("close-bookmark");
android.removeBookmark(bookmark.value.id);
}
}

async function adjustRange() {
const documentId = props.documentId;
const [startOrdinal, endOrdinal] = bookmark.value.ordinalRange;
const [startOff, endOff] = bookmark.value.offsetRange;
const firstElem = document.querySelector(`#doc-${documentId} #v-${startOrdinal}`);
const secondElem = document.querySelector(`#doc-${documentId} #v-${endOrdinal}`);
if (firstElem === null || secondElem === null) {
console.error("Element is not found!", documentId, startOrdinal, endOrdinal);
return;
}
const [first, startOff1] = findNodeAtOffsetWithNullOffset(firstElem, startOff);
const [second, endOff1] = findNodeAtOffsetWithNullOffset(secondElem, endOff);
emit(Events.CLOSE_MODALS);
await nextTick();

const range = new Range();
range.setStart(first, startOff1);
range.setEnd(second, endOff1);

const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
//android.adjustRange();
console.log("clicking", firstElem, sel, range);
//firstElem.click();
}

return {
toggleWholeVerse, shareVerse, assignLabels, removeBookmark, areYouSure, labels, openStudyPad,
...useCommon()
adjustRange, ...useCommon()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
// eslint-disable-next-line no-unused-vars,vue/no-setup-props-destructure
const {id, bookInitials, bookmarks, ordinalRange, originalOrdinalRange, v11n} = props.document;

provide("bibleDocumentInfo", {ordinalRange, originalOrdinalRange, v11n})
provide("bibleDocumentInfo", {id, ordinalRange, originalOrdinalRange, v11n})

const globalBookmarks = inject("globalBookmarks");
globalBookmarks.updateBookmarks(...bookmarks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<AmbiguousSelectionBookmarkButton
v-else-if="bookmarkMap.has(s.options.bookmarkId)"
:bookmark-id="s.options.bookmarkId"
:document-id="s.options.documentId"
@selected="selected(s)"
/>
</template>
Expand Down Expand Up @@ -85,7 +86,7 @@ export default {
if(eventFunctions.length > 0) {
if(eventFunctions.length === 1) {
if(eventFunctions[0].options.bookmarkId) {
emit(Events.BOOKMARK_CLICKED, eventFunctions[0].options.bookmarkId);
emit(Events.BOOKMARK_CLICKED, eventFunctions[0].options.bookmarkId, eventFunctions[0].options.documentId);
} else {
eventFunctions[0].callback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<div style="height: 7px"/>
<BookmarkButtons
:bookmark="bookmark"
:document-id="documentId"
show-study-pad-buttons
@info-clicked="editNotes"
/>
Expand All @@ -45,6 +46,7 @@ export default {
name: "AmbiguousSelectionBookmarkButton",
props: {
bookmarkId: {required: true, type: Number},
documentId: {required: true, type: String},
},
components: {LabelList, BookmarkButtons},
setup(props, {emit: $emit}) {
Expand All @@ -71,17 +73,17 @@ export default {

function editNotes() {
$emit("selected");
emit(Events.BOOKMARK_CLICKED, bookmark.value.id, {open: true});
emit(Events.BOOKMARK_CLICKED, bookmark.value.id, props.documentId, {open: true});
}

function openBookmark() {
$emit("selected");
emit(Events.BOOKMARK_CLICKED, bookmark.value.id);
emit(Events.BOOKMARK_CLICKED, bookmark.value.id, props.documentId);
}

function bookmarkInfo() {
$emit("selected");
emit(Events.BOOKMARK_CLICKED, bookmark.value.id, {openInfo: true});
emit(Events.BOOKMARK_CLICKED, bookmark.value.id, props.documentId, {openInfo: true});
}

return {
Expand Down
7 changes: 5 additions & 2 deletions app/bibleview-js/src/components/modals/BookmarkModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
</div>
<BookmarkButtons
:bookmark="bookmark"
:document-id="documentId"
@close-bookmark="showBookmark = false"
@info-clicked="infoShown = false"
/>
Expand Down Expand Up @@ -107,6 +108,7 @@ export default {
const areYouSure = ref(null);
const infoShown = ref(false);
const bookmarkId = ref(null);
const documentId = ref(null);

const {bookmarkMap, bookmarkLabels} = inject("globalBookmarks");

Expand All @@ -123,8 +125,9 @@ export default {
const bookmarkNotes = computed(() => bookmark.value.notes);
let originalNotes = null;

setupEventBusListener(Events.BOOKMARK_CLICKED, (bookmarkId_, {openInfo = false, open = false} = {}) => {
setupEventBusListener(Events.BOOKMARK_CLICKED, (bookmarkId_, documentId_, {openInfo = false, open = false} = {}) => {
bookmarkId.value = bookmarkId_;
documentId.value = documentId_;
originalNotes = bookmarkNotes.value;
//if(!showBookmark.value) infoShown.value = false;
infoShown.value = openInfo;
Expand Down Expand Up @@ -158,7 +161,7 @@ export default {

return {
showBookmark, closeBookmark, areYouSure, infoShown, bookmarkNotes, bookmark, labelColor,
changeNote, labels, originalBookLink, strings, adjustedColor, editDirectly, ...common
changeNote, labels, originalBookLink, strings, adjustedColor, editDirectly, documentId, ...common
};
},
}
Expand Down
5 changes: 5 additions & 0 deletions app/bibleview-js/src/composables/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export function useAndroid({bookmarks}, config) {
android.openStudyPad(labelId);
}

function adjustRange() {
android.adjustRange();
}

function updateOrderNumber(labelId, bookmarks, journals) {
const orderNumberPairs = l => l.map(v=>[v.id, v.orderNumber])
android.updateOrderNumber(labelId, JSON.stringify(
Expand Down Expand Up @@ -289,6 +293,7 @@ export function useAndroid({bookmarks}, config) {
toggleBookmarkLabel,
reportModalState,
setBookmarkWholeVerse,
adjustRange,
}

if(config.developmentMode) return {
Expand Down
8 changes: 4 additions & 4 deletions app/bibleview-js/src/composables/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export function useBookmarks(documentId,

function addBookmarkEventFunctions(event) {
for (const b of bookmarks) {
addEventFunction(event, null, {bookmarkId: b.id});
addEventFunction(event, null, {bookmarkId: b.id, documentId});
}
}

Expand Down Expand Up @@ -424,7 +424,7 @@ export function useBookmarks(documentId,
const iconElement = getIconElement(speakIcon, color);

iconElement.addEventListener("click", event => addEventFunction(event,
null, {bookmarkId: b.id}));
null, {bookmarkId: b.id, documentId}));
firstElement.parentElement.insertBefore(iconElement, firstElement);
undoHighlights.push(() => iconElement.remove());
}
Expand All @@ -436,7 +436,7 @@ export function useBookmarks(documentId,
const iconElement = getIconElement(b.notes ? editIcon : bookmarkIcon, color);

iconElement.addEventListener("click", event => addEventFunction(event,
null, {bookmarkId: b.id}));
null, {bookmarkId: b.id, documentId}));
lastElement.parentNode.insertBefore(iconElement, lastElement.nextSibling);
undoHighlights.push(() => iconElement.remove());
}
Expand All @@ -459,7 +459,7 @@ export function useBookmarks(documentId,
const iconElement = getIconElement(bookmarkIcon, color);
iconElement.addEventListener("click", event => {
for(const b of bookmarkList) {
addEventFunction(event, null, {bookmarkId: b.id});
addEventFunction(event, null, {bookmarkId: b.id, documentId});
}
});
if(bookmarkList.length>1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,10 @@ class BibleJavascriptInterface(
if(value) ABEventBus.getDefault().post(ToastEvent(R.string.whole_verse_turned_on))
}

@JavascriptInterface
fun adjustRange() {
bibleView.adjustRange()
}

private val TAG get() = "BibleView[${bibleView.windowRef.get()?.id}] JSInt"
}
31 changes: 20 additions & 11 deletions app/src/main/java/net/bible/android/view/activity/page/BibleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -356,30 +356,39 @@ class BibleView(val mainBibleActivity: MainBibleActivity,
if(removeRanges) executeJavascriptOnUiThread("bibleView.emit('remove_ranges')")
}

fun adjustRange() {
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// runOnUiThread {
// mainBibleActivity.startActionMode(ActionModeCallback2(null), ActionMode.TYPE_FLOATING)
// //startActionMode(null, ActionMode.TYPE_FLOATING)
// }
//}
}

@RequiresApi(Build.VERSION_CODES.M)
private inner class ActionModeCallback2(val callback: ActionMode.Callback): ActionMode.Callback2() {
private inner class ActionModeCallback2(val callback: ActionMode.Callback?): ActionMode.Callback2() {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val wasUpdated2 = callback.onCreateActionMode(mode, menu)
val wasUpdated2 = callback?.onCreateActionMode(mode, menu)?: false
val wasUpdated1 = false
return wasUpdated1 || wasUpdated2
}

override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
if(!actionModeEnabled) return true
val wasUpdated2 = callback.onPrepareActionMode(mode, menu)
val wasUpdated2 = callback?.onPrepareActionMode(mode, menu)?:false
val wasUpdated1 = onPrepareActionMenu(mode, menu)
return wasUpdated1 || wasUpdated2
}

override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
val handled1 = onActionMenuItemClicked(mode, item)
val handled2 = callback.onActionItemClicked(mode, item)
val handled2 = callback?.onActionItemClicked(mode, item)?:false
if(handled1) stopSelection(true)
return handled1 || handled2
}

override fun onDestroyActionMode(mode: ActionMode) {
return callback.onDestroyActionMode(mode)
callback?.onDestroyActionMode(mode)
}

override fun onGetContentRect(mode: ActionMode, view: View, outRect: Rect) {
Expand All @@ -391,7 +400,7 @@ class BibleView(val mainBibleActivity: MainBibleActivity,
}
}

override fun startActionMode(callback: ActionMode.Callback, type: Int): ActionMode {
override fun startActionMode(callback: ActionMode.Callback?, type: Int): ActionMode {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
super.startActionMode(ActionModeCallback2(callback), type)
} else {
Expand All @@ -404,25 +413,25 @@ class BibleView(val mainBibleActivity: MainBibleActivity,
}

// This can be removed after Lollipop support is dropped.
private inner class ActionModeCallback(val callback: ActionMode.Callback): ActionMode.Callback {
private inner class ActionModeCallback(val callback: ActionMode.Callback?): ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
callback.onCreateActionMode(mode, menu)
callback?.onCreateActionMode(mode, menu)
return true
}

override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
if(!actionModeEnabled) return true
val wasUpdated1 = callback.onPrepareActionMode(mode, menu)
val wasUpdated1 = callback?.onPrepareActionMode(mode, menu)?: false
val wasUpdated2 = onPrepareActionMenu(mode, menu)
return wasUpdated1 || wasUpdated2
}

override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return onActionMenuItemClicked(mode, item) || callback.onActionItemClicked(mode, item)
return onActionMenuItemClicked(mode, item) || callback?.onActionItemClicked(mode, item)?: false
}

override fun onDestroyActionMode(mode: ActionMode?) {
return callback.onDestroyActionMode(mode)
callback?.onDestroyActionMode(mode)
}
}

Expand Down