Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 85ea8c6

Browse files
authored
Fix initial cursor in empty doc (#83)
1 parent a8d4146 commit 85ea8c6

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

libreoffice-core/desktop/inc/lib/wasm_extensions.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct DESKTOP_DLLPUBLIC WasmDocumentExtension : public _LibreOfficeKitDocument
109109
std::vector<std::pair<const std::string, const std::string>> listParts() const;
110110

111111
std::vector<std::pair<std::string, std::string>> save();
112-
112+
std::optional<std::string> getCursor(int viewId);
113113
};
114114

115115
struct DESKTOP_DLLPUBLIC WasmOfficeExtension : public _LibreOfficeKit

libreoffice-core/desktop/source/app/main_wasm.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,8 @@ class DocumentClient final : public INotifier
850850

851851
val getRedlineTextRange(int redlineId) { return writer()->getRedlineTextRange(redlineId); }
852852

853+
std::optional<std::string> getCursor(int viewId) { return ext()->getCursor(viewId); }
854+
853855
private:
854856
struct DocWithId
855857
{
@@ -1032,5 +1034,6 @@ EMSCRIPTEN_BINDINGS(lok)
10321034
.function("undo", &DocumentClient::undo)
10331035
.function("redo", &DocumentClient::redo)
10341036
.function("getRedlineTextRange", &DocumentClient::getRedlineTextRange)
1037+
.function("getCursor", &DocumentClient::getCursor)
10351038
.function("newView", &DocumentClient::newView);
10361039
}

libreoffice-core/desktop/source/lib/wasm_extensions.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,5 +368,21 @@ std::vector<std::pair<std::string, std::string>> WasmDocumentExtension::save()
368368
auto files = comphelper::OStorageHelper::GetExpandedStorageInstance()->getRecentlyChangedFiles();
369369
return files;
370370
}
371+
372+
std::optional<std::string> WasmDocumentExtension::getCursor(int viewId)
373+
{
374+
if (SfxViewShell* viewShell
375+
= SfxViewShell::GetFirst(false, [viewId](const SfxViewShell* shell)
376+
{ return shell->GetViewShellId().get() == viewId; }))
377+
{
378+
std::optional<OString> payload
379+
= viewShell->getLOKPayload(LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR, viewId);
380+
if (payload)
381+
{
382+
return std::string(static_cast<std::string_view>(*payload));
383+
}
384+
}
385+
return {};
371386
}
372387

388+
}

libreoffice-core/desktop/wasm/shared.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export type DocumentWithViewMethods = {
189189
getExpandedPart(path: string): {path: string, content: ArrayBuffer} | null;
190190
listExpandedParts(): Array<{path: string, sha: string}>;
191191
getRedlineTextRange(id: number): RectArray[] | undefined;
192+
getCursor(): string | undefined;
192193
};
193194

194195
/** methods that forward to a class weakly bound to the Document that forwards calls */

libreoffice-core/desktop/wasm/soffice.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ export declare class Document {
271271
redo(count: number): void;
272272

273273
getRedlineTextRange(id: number): RectArray[] | undefined;
274+
275+
getCursor(viewId: number): string | undefined;
274276
}
275277

276278
// NOTE: Disabled until unoembind startup cost is under 1s

libreoffice-core/desktop/wasm/worker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,11 @@ const handler: DocumentMethodHandler<Document> = {
639639
getRedlineTextRange: function (doc: Document, viewId: ViewId, id: number) {
640640
doc.setCurrentView(viewId);
641641
return doc.getRedlineTextRange(id);
642-
}
642+
},
643+
644+
getCursor: function (doc: Document, viewId: ViewId) {
645+
return doc.getCursor(viewId);
646+
},
643647
};
644648

645649
const forwarding: ForwardingMethodHandlers<Document> = {

qa-env/src/OfficeDocument/cursorSignal.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { CallbackType } from '@lok/lok_enums';
22
import { DocumentClient } from '@lok/shared';
33
import { Accessor } from 'solid-js';
4-
import {
5-
createDocEventSignal,
6-
createInitalizedDocEventSignal,
7-
} from './docEventSignal';
4+
import { createInitalizedDocEventSignal } from './docEventSignal';
85

96
const cursorVisibleMap = new WeakMap<DocumentClient, Accessor<boolean>>();
107
export function getOrCreateCursorVisible(
@@ -42,25 +39,28 @@ const cursorPositionMap = new WeakMap<
4239
DocumentClient,
4340
Accessor<CursorRectTwips | undefined>
4441
>();
42+
4543
export function getOrCreateCursorPosition(
4644
doc: Accessor<DocumentClient>
4745
): Accessor<CursorRectTwips | undefined> {
4846
const doc_ = doc();
4947
let result = cursorPositionMap.get(doc_);
48+
const cursorTransform = (payload: string | undefined) => {
49+
if (!payload) return undefined as any;
50+
const { viewId } = doc_;
51+
const event: ParsedInvalidateVisibleCursor = JSON.parse(payload);
52+
// if the view ID doesn't match, don't set the signal
53+
if (viewId !== Number(event.viewId)) return undefined;
54+
55+
const rect = event.rectangle.split(', ').map(toInt);
56+
return rect as CursorRectTwips;
57+
};
5058
if (result == null) {
51-
result = createDocEventSignal(
59+
result = createInitalizedDocEventSignal(
5260
doc,
5361
CallbackType.INVALIDATE_VISIBLE_CURSOR,
54-
(payload) => {
55-
const { viewId } = doc_;
56-
const event: ParsedInvalidateVisibleCursor = JSON.parse(payload);
57-
// if the view ID doesn't match, don't set the signal
58-
if (viewId !== Number(event.viewId)) return undefined;
59-
60-
const rect = event.rectangle.split(', ').map(toInt);
61-
return rect as CursorRectTwips;
62-
},
63-
true
62+
cursorTransform,
63+
doc_.getCursor().then(cursorTransform)
6464
);
6565
}
6666
return result;

qa-env/src/OfficeDocument/docEventSignal.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ export function createInitalizedDocEventSignal<T>(
4747
doc: Accessor<DocumentClient>,
4848
type: CallbackType,
4949
transform: (payload: string) => T | undefined,
50-
initial: Exclude<T, Function>
50+
initial: Exclude<T, Function> | Promise<Exclude<T, Function>>
5151
): Accessor<T> {
52-
const [payload, setPayload] = createSignal<T>(initial);
52+
const [payload, setPayload] =
53+
initial instanceof Promise
54+
? createSignal<T>(undefined as T)
55+
: createSignal<T>(initial);
56+
if (initial instanceof Promise) {
57+
initial.then(setPayload);
58+
}
5359
const callback = frameThrottle(
5460
transform
5561
? (payload: string) => {

0 commit comments

Comments
 (0)