-
Notifications
You must be signed in to change notification settings - Fork 108
[CLX-3438][Horizon] Notebook changes #3427
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
e66845d
df0abb3
48ae3f2
215c891
142fab6
f7a7205
f676ccd
c1a142e
13366b1
088594e
54ab8c3
e045726
3177b70
1c2e97d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,13 @@ import com.instructure.canvasapi2.utils.weave.catch | |
| import com.instructure.canvasapi2.utils.weave.tryLaunch | ||
| import com.instructure.horizon.R | ||
| import com.instructure.horizon.features.notebook.addedit.AddEditViewModel | ||
| import com.instructure.horizon.features.notebook.common.composable.toNotebookLocalisedDateFormat | ||
| import com.instructure.horizon.features.notebook.common.model.NotebookType | ||
| import com.instructure.horizon.features.notebook.navigation.NotebookRoute | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.update | ||
| import java.util.Date | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
|
|
@@ -57,6 +59,7 @@ class AddNoteViewModel @Inject constructor( | |
| it.copy( | ||
| title = context.getString(R.string.createNoteTitle), | ||
| hasContentChange = true, | ||
| lastModifiedDate = Date().toNotebookLocalisedDateFormat(), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a new |
||
| highlightedData = NoteHighlightedData( | ||
| selectedText = highlightedText, | ||
| range = NoteHighlightedDataRange( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import androidx.compose.runtime.getValue | |
| import androidx.compose.runtime.mutableIntStateOf | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.runtime.setValue | ||
|
|
@@ -45,6 +46,7 @@ import com.instructure.horizon.features.notebook.common.model.Note | |
| import com.instructure.horizon.features.notebook.common.model.NotebookType | ||
| import com.instructure.horizon.features.notebook.common.webview.JSTextSelectionInterface.Companion.addTextSelectionInterface | ||
| import com.instructure.horizon.features.notebook.common.webview.JSTextSelectionInterface.Companion.evaluateTextSelectionInterface | ||
| import com.instructure.horizon.features.notebook.common.webview.JSTextSelectionInterface.Companion.getNoteYPosition | ||
| import com.instructure.horizon.features.notebook.common.webview.JSTextSelectionInterface.Companion.highlightNotes | ||
| import com.instructure.pandautils.compose.composables.ComposeEmbeddedWebViewCallbacks | ||
| import com.instructure.pandautils.compose.composables.ComposeWebViewCallbacks | ||
|
|
@@ -53,6 +55,7 @@ import com.instructure.pandautils.utils.HtmlContentFormatter | |
| import com.instructure.pandautils.utils.JsExternalToolInterface | ||
| import com.instructure.pandautils.utils.JsGoogleDocsInterface | ||
| import com.instructure.pandautils.views.CanvasWebView | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.launch | ||
|
|
||
|
|
@@ -69,7 +72,8 @@ fun ComposeNotesHighlightingCanvasWebView( | |
| applyOnWebView: (CanvasWebView.() -> Unit)? = null, | ||
| webViewCallbacks: ComposeWebViewCallbacks = ComposeWebViewCallbacks(), | ||
| embeddedWebViewCallbacks: ComposeEmbeddedWebViewCallbacks? = null, | ||
| scrollState: ScrollState? = null | ||
| scrollState: ScrollState? = null, | ||
| scrollToNoteId: String? = null | ||
| ) { | ||
| var pageHeight by remember { mutableIntStateOf(0) } | ||
| var scrollValue by rememberSaveable { mutableIntStateOf(0) } | ||
|
|
@@ -78,6 +82,7 @@ fun ComposeNotesHighlightingCanvasWebView( | |
| val webViewState = rememberSaveable { bundleOf() } | ||
| val selectionLocation: MutableStateFlow<SelectionLocation> by remember { mutableStateOf(MutableStateFlow(SelectionLocation(0f, 0f, 0f, 0f))) } | ||
| val lifecycleOwner = LocalLifecycleOwner.current | ||
| val composeScope = rememberCoroutineScope() | ||
| val context = LocalContext.current | ||
| val clipboardManager: ClipboardManager = LocalClipboardManager.current | ||
|
|
||
|
|
@@ -91,6 +96,10 @@ fun ComposeNotesHighlightingCanvasWebView( | |
| var selectedTextStart by remember { mutableIntStateOf(0) } | ||
| var selectedTextEnd by remember { mutableIntStateOf(0) } | ||
|
|
||
| var isScrolled by rememberSaveable { mutableStateOf(false) } | ||
| var isPageLoaded by remember { mutableStateOf(false) } | ||
| var webViewInstance by remember { mutableStateOf<NotesHighlightingCanvasWebViewWrapper?>(null) } | ||
|
|
||
| LaunchedEffect(scrollState?.maxValue) { | ||
| val maxValue = scrollState?.maxValue ?: 0 | ||
|
|
||
|
|
@@ -100,14 +109,30 @@ fun ComposeNotesHighlightingCanvasWebView( | |
| } | ||
| } | ||
|
|
||
| LaunchedEffect(scrollToNoteId, isPageLoaded) { | ||
|
||
| delay(500) | ||
| val noteId = scrollToNoteId | ||
|
||
| if (noteId != null && isPageLoaded && scrollState != null && webViewInstance != null && !isScrolled) { | ||
| isScrolled = true | ||
| webViewInstance?.webView?.getNoteYPosition(noteId) { yPosition -> | ||
| if (yPosition != null) { | ||
| composeScope.launch { | ||
| val targetScroll = yPosition.toInt().coerceIn(0, scrollState.maxValue) | ||
| scrollState.animateScrollTo(targetScroll) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var previousHeight by remember { mutableIntStateOf(0) } | ||
| if (LocalInspectionMode.current) { | ||
| Text(text = content) | ||
| } else { | ||
| AndroidView( | ||
| factory = { | ||
| scrollValue = scrollState?.value ?: 0 | ||
| NotesHighlightingCanvasWebViewWrapper( | ||
| val wrapper = NotesHighlightingCanvasWebViewWrapper( | ||
| it, | ||
| callback = AddNoteActionModeCallback( | ||
| lifecycleOwner, | ||
|
|
@@ -161,6 +186,7 @@ fun ComposeNotesHighlightingCanvasWebView( | |
|
|
||
| webView.evaluateTextSelectionInterface() | ||
| webView.highlightNotes(notesStateValue.value) | ||
| isPageLoaded = true | ||
| } | ||
|
|
||
| override fun onPageStartedCallback(webView: WebView, url: String) = webViewCallbacks.onPageStarted(webView, url) | ||
|
|
@@ -184,6 +210,8 @@ fun ComposeNotesHighlightingCanvasWebView( | |
|
|
||
| applyOnWebView?.let { applyOnWebView -> webView.applyOnWebView() } | ||
| } | ||
| webViewInstance = wrapper | ||
| wrapper | ||
| }, | ||
| update = { | ||
| if (webViewState.isEmpty) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -678,6 +678,7 @@ function highlightSelection(paramsJson) { | |
| const highlightElement = document.createElement("span"); | ||
| highlightElement.classList.add(highlightClassName); | ||
| highlightElement.classList.add(cssClass); | ||
| highlightElement.setAttribute('data-note-id', noteId); | ||
| highlightElement.onclick = function () { ${ JS_INTERFACE_NAME }.onHighlightedTextClicked(noteId, noteReactionString, selectedText, userComment, startOffset, startContainer, endOffset, endContainer, textSelectionStart, textSelectionEnd, updatedAt); }; | ||
| highlightElement.textContent = textNode.textContent; | ||
|
|
||
|
|
@@ -687,6 +688,17 @@ function highlightSelection(paramsJson) { | |
| } | ||
|
|
||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The const highlight = document.querySelector(`[data-note-id="${noteId}"]`);
if (highlight) {
const rect = highlight.getBoundingClientRect();
return rect.top + window.scrollY;
}
return -1; |
||
| function getNotePosition(noteId) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| const highlights = document.getElementsByClassName('notebook-highlight'); | ||
| for (const highlight of highlights) { | ||
| if (highlight.getAttribute('data-note-id') === noteId) { | ||
| const rect = highlight.getBoundingClientRect(); | ||
| return rect.top + window.scrollY; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| javascript: (function () { | ||
| document.addEventListener("selectionchange", () => { | ||
| const selection = window.getSelection(); | ||
|
|
@@ -771,5 +783,13 @@ javascript: (function () { | |
| this.evaluateJavascript(script, null) | ||
| } | ||
| } | ||
|
|
||
| fun WebView.getNoteYPosition(noteId: String, onResult: (Float?) -> Unit) { | ||
| val quotedNoteId = JSONObject.quote(noteId) | ||
| this.evaluateJavascript("getNotePosition($quotedNoteId)") { result -> | ||
| val position = result?.replace("\"", "")?.toFloatOrNull() | ||
| onResult(if (position != null && position >= 0) position else null) | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
onNoteAddedcallback now callsuiState.addNote()instead of navigating directly. This is good architectural change, but ensure that:addNotefunction properly handles all the navigation and state managementCould you verify that
uiState.addNoteis implemented and handles these cases?