Commit Graph

40 Commits

Author SHA1 Message Date
Henrik Giesel
a981e56008
Improved add-on extension API (#1626)
* Add componentHook functionality

* Register package NoteEditor

* Rename OldEditorAdapter to NoteEditor

* Expose instances in component-hook as well

* Rename NoteTypeButtons to NotetypeButtons

* Move PreviewButton initialization to BrowserEditor.svelte

* Remove focusInRichText

- Same thing can be done by inspecting activeInput

* Satisfy formatter

* Fix remaining rebase issues

* Add .bazel to .prettierignore

* Rename currentField and activeInput to focused{Field,Input}

* Move identifier to lib and registration to sveltelib

* Fix Dynamic component insertion

* Simplify editingInputIsRichText

* Give extra warning in svelte/svelte.ts

- This was caused by doing a rename of a files, that only differed in
  case: NoteTypeButtons.svelte to NotetypeButtons.svelte
- It was quite tough to figure out, and this console.log might make it
  easier if it ever happens again

* Change signature of contextProperty

* Add ts/typings for add-on definition files

* Add Anki types in typings/common/index.d.ts

* Export without .svelte suffix

It conflicts with how Svelte types its packages

* Fix left over .svelte import from editor.py

* Rename NoteTypeButtons to unrelated to ensure case-only rename

* Rename back to NotetypeButtons.svelte

* Remove unused component-hook.ts, Fix typing in lifecycle-hooks

* Merge runtime-require and register-package into one file

+ Give some preliminary types to require

* Rename uiDidLoad to loaded

* Fix eslint / svelte-check

* Rename context imports to noteEditorContext

* Fix import name mismatch

- I wonder why these issues are not caught by svelte-check?

* Rename two missed usages of uiDidLoad

* Fix ButtonDropdown from having wrong border-radius

* Uniformly rename libraries to packages

- I don't have a strong opinion on whether to name them libraries or
  packages, I just think we should have a uniform name.
- JS/TS only uses the terms "module" and "namespace", however `package`
  is a reserved keyword for future use, whereas `library` is not.

* Refactor registration.ts into dynamic-slotting

- This is part of an effort to refactor the dynamic slotting (extending
  buttons) functionality out of components like ButtonGroup.

* Remove dynamically-slottable logic from ButtonToolbar

* Use DynamicallySlottable in editor-toolbar

* Fix no border radius on indentation button dropdown

* Fix AddonButtons

* Remove Item/ButtonGroupItem in deck-options, where it's not necessary

* Remove unnecessary uses of Item and ButtonGroupItem

* Fix remaining tests

* Fix relative imports

* Revert change return value of remapBinToSrcDir to ./bazel/out...

* Remove typings directory

* Adjust comments for dynamic-slottings
2022-02-03 14:52:11 +10:00
Damien Elmes
3fc81e5f15 another external workspace fix for sass 2021-11-12 15:02:17 +10:00
Damien Elmes
84021976d5 speed up Svelte compile
Approx 27s -> 16s when rebuilding editor after making a change to
lib. Still slower than I'd like.
2021-11-04 15:18:28 +10:00
Damien Elmes
9151fd93d4 allow passing npm repo name in compile_svelte() 2021-10-31 18:10:11 +10:00
Henrik Giesel
0dff5ea3a3
Use trailingComma: all setting in .prettierrc (#1435) 2021-10-19 09:06:00 +10:00
Damien Elmes
fed5c3da0e drop ts_library() requirement in ts/svelte 2021-10-18 13:00:55 +10:00
Damien Elmes
1f876cfe39 Svelte build improvements
1. All Svelte files in a package are compiled in one step now, which
ensures that properties that use types from a different Svelte file in the
same package are typed correctly. The single-file svelte() has been removed,
and compile_svelte() may be renamed to svelte() in the future.

2. The .ts files in the same package are included as part of the Svelte
compilation, so that types imported imported from .ts files in the
same package work.

3. Dependencies passed into the rule are now loaded into the TypeScript
compiler, so that properties referencing types from different packages
work. We'll need to update our compile_svelte() lines to list the
dependencies. For example, before this change:

% cat bazel-bin/ts/congrats/CongratsPage.svelte.d.ts
import { SvelteComponentTyped } from "svelte";
declare const __propDef: {
    props: {
        info: any;
    };
...

After adding //ts/lib to the deps of compile_svelte() in ts/congrats:

% cat bazel-bin/ts/congrats/CongratsPage.svelte.d.ts
import { SvelteComponentTyped } from "svelte";
import type { Scheduler } from "../lib/proto";
declare const __propDef: {
    props: {
        info: Scheduler.CongratsInfoResponse;
    };
...
2021-10-18 12:44:29 +10:00
Damien Elmes
7128de895f declare bootstrap prereq in Svelte compile
Mistakenly stripped this out when removing the typescript dependencies
2021-10-13 11:48:22 +10:00
Henrik Giesel
c64bac57a6
Put sass into repo directory (#1409)
Fix Sass build
2021-10-09 10:25:03 +10:00
Damien Elmes
79ceb89637 svelte needs its shims to generate prop typing
I erroneously removed them near the end of the ts_project work, and
didn't realise the properties had broken.
2021-10-07 21:33:21 +10:00
Damien Elmes
a3d9f90af5 update to latest rules_nodejs & switch to ts_project
ts_library() is deprecated and will presumably be dropped from a
future rules_nodejs, and it wasn't working with the jest tests
after updating, so we switch over to ts_project().

There are some downsides:

- It's a bit slower, as the worker mode doesn't appear to function
at the moment.
- Getting it working with a mix of source files and generated files
was quite tricky, especially as things behave differently on Windows,
and differently when editing with VS Code. Solved with a small patch
to the rules, and a wrapper script that copies everything into the
bin folder first. To keep VS Code working correctly as well, the built
files are symlinked into the source folder.
- TS libraries are not implicitly linked to node_modules, so they
can't be imported with an absolute name like "lib/proto" - we need
to use relative paths like "../lib/proto" instead. Adjusting "paths"
in tsconfig.json makes it work for TS compilation, but then it fails
at the esbuild stage. We could resolve it by wrapping the TS
libraries in a subsequent js_library() call, but that has the downside
of losing the transient dependencies, meaning they need to be listed
again.  Alternatively we might be able to solve it in the future by
adjusting esbuild, but for now the paths have been made relative to
keep things simple.

Upsides:

- Along with updates to the Svelte tooling, Svelte typing has improved.
All exports made in a Svelte file are now visible to other files that
import them, and we no longer rebuild the Svelte files when TS files
are updated, as the Svelte files do no type checking themselves, and
are just a simple transpilation. Svelte-check now works on Windows again,
and there should be no errors when editing in VS Code after you've
built the project. The only downside seems to be that cmd+clicking
on a Svelte imports jumps to the .d.ts file instead of the original now;
presumably they'll fix that in a future plugin update.
- Each subfolder now has its own tsconfig.json, and tsc can be called
directly for testing purposes (but beware it will place build products
in the source tree): ts/node_modules/.bin/tsc -b ts
- We can drop the custom esbuild_toolchain, as it's included in the
latest rules_nodejs.

Other changes:

- "image_module_support" is moved into lib/, and imported with
<reference types=...>
- Images are now imported directly from their npm package; the
extra copy step has been removed.

Windows users may need to use "bazel clean" before building this,
due to old files lying around in the build folder.
2021-10-01 12:52:53 +10:00
Damien Elmes
04ba24cb54 silence execCommand deprecation warnings for now 2021-09-29 11:36:37 +10:00
Damien Elmes
e660e0a3e5 update svelte2tsx
We can now use the typings from the Svelte library instead of the
svelte2tsx shims
2021-09-25 11:07:31 +10:00
Damien Elmes
94c4f99343 update ts deps
typescript and bootstrap have been pinned for now:

https://github.com/ankitects/anki/issues/1386
https://github.com/ankitects/anki/issues/1385

hint failures for svelte-check have also been temporarily turned
off, due to it now complaining about document.execCommand():

Hint: The signature '(commandId: string, showUI?: boolean, value?: string): boolean' of 'document.execCommand' is deprecated. (ts)
    const wrapWithForecolor = (color: string) => () => {
        document.execCommand("forecolor", false, color);
    };

Will follow up in #1377
2021-09-22 23:21:39 +10:00
Henrik Giesel
677a33f585 Fix for svelte_check 2021-09-02 23:37:17 +02:00
Henrik Giesel
8ff8186aab Uniformly shorten SCSS imports/uses 2021-09-02 23:37:17 +02:00
Henrik Giesel
4a2d2a75c1 Directly include CSS from bootstrap and codemirror packages 2021-09-02 23:37:16 +02:00
Damien Elmes
372a28be29 rename workspace
I'd forgotten that Bazel "helpfully" adds __init__.py files into folders
that match the workspace name, breaking imports.
2021-07-10 23:58:53 +10:00
Damien Elmes
616db33c0e refactor protobuf handling for split/import
In order to split backend.proto into a more manageable size, the protobuf
handling needed to be updated. This took more time than I would have
liked, as each language handles protobuf differently:

- The Python Protobuf code ignores "package" directives, and relies
solely on how the files are laid out on disk. While it would have been
nice to keep the generated files in a private subpackage, Protobuf gets
confused if the files are located in a location that does not match
their original .proto layout, so the old approach of storing them in
_backend/ will not work. They now clutter up pylib/anki instead. I'm
rather annoyed by that, but alternatives seem to be having to add an extra
level to the Protobuf path, making the other languages suffer, or trying
to hack around the issue by munging sys.modules.
- Protobufjs fails to expose packages if they don't start with a capital
letter, despite the fact that lowercase packages are the norm in most
languages :-( This required a patch to fix.
- Rust was the easiest, as Prost is relatively straightforward compared
to Google's tools.

The Protobuf files are now stored in /proto/anki, with a separate package
for each file. I've split backend.proto into a few files as a test, but
the majority of that work is still to come.

The Python Protobuf building is a bit of a hack at the moment, hard-coding
"proto" as the top level folder, but it seems to get the job done for now.

Also changed the workspace name, as there seems to be a number of Bazel
repos moving away from the more awkward reverse DNS naming style.
2021-07-10 19:17:05 +10:00
Damien Elmes
84e2cfbe3f add a hack to handle sass resolution in svelte in external workspace 2021-05-20 15:32:06 +10:00
Damien Elmes
a564399414 disable svelte-check on Windows for now 2021-05-19 11:45:02 +10:00
Damien Elmes
220e14bf0f disable broken svelte_check test on Windows for now
Likely caused by a lack of sandboxing on Windows, causing the generated
.d.ts files to be visible to svelte_check, and being picked up in
preference over the .svelte file.
2021-05-08 12:15:49 +10:00
Damien Elmes
4951ef12c9 fix properties missing from generated Svelte .d.ts files
They're currently being set to 'any', as we're not providing the dependencies
to the tsc invocation. Older Anki versions had the same issue, and we'll
want to fix that as well, but for now this at least restores the missing
props.
2021-05-04 18:55:58 +10:00
Henrik Giesel
a7c3aec5dc Update Typescript to 4.2.4 2021-04-22 18:33:42 +02:00
Henrik Giesel
b1de095162 Include editor-toolbar as a library in editor 2021-04-20 13:44:44 +02:00
Damien Elmes
a6ed8e90ce support passing @types/... into svelte-check 2021-04-16 23:18:33 +10:00
Damien Elmes
6cee43631a allow passing sass deps to svelte compile/check 2021-04-15 14:27:53 +10:00
Damien Elmes
e520e8df02 add missing copyright headers to *.ts 2021-04-13 18:57:08 +10:00
Damien Elmes
6e4cdc52a6 stop Svelte warnings from failing the build
+ check for them at test time
2021-04-12 14:28:09 +10:00
Henrik Giesel
7655f14859 Satisfy svelte_check 2021-03-22 15:41:43 +01:00
Damien Elmes
dbe5d43ba0 bundle all Svelte css into separate file
- svelte compilation outputs a separate .css file for each component
- compilation also adds an "import foo.css" to the top of each generated
.mjs file
- when the .mjs files are bundled into app.js, esbuild creates an app.css
as well
- graphs.scss was renamed to graphs_shared.scss and imported in the
top level GraphsPage. Henrik's style refactoring would be a better path
forward, but I needed to make this change for now, as the filenames were
conflicting.
2021-03-21 16:06:36 +10:00
Damien Elmes
5403f704d0 ESM import issue on Windows came back again
Not sure if I just missed this in the previous change, or whether
the patch_resolver change broke things again.

Previous issue:
e60eb27184
2021-03-21 16:06:07 +10:00
Damien Elmes
e857aa2ea9 output embedded Svelte css into separate .css file for bundling 2021-03-21 16:06:01 +10:00
Damien Elmes
2658d9992e fixes for Svelte compilation
- enable resolver patch on worker binary to ensure js imports work
on subsequent worker requests
- cache ts library content, and use unified interface for cache
- prepare for separate css outputs
2021-03-21 16:05:46 +10:00
Damien Elmes
e0b85865f4 avoid temporary .ts file due to lack of sandbox on Windows
Just a quick hack for now to store it in memory, as the temp file
conflicts on Windows due to the lack of a sandbox, and we don't really
have a need to write it to the filesystem anyway.
2021-03-21 00:17:09 +10:00
Damien Elmes
aabcc8a7db use ts language service for much faster Svelte compiles 2021-03-20 23:49:10 +10:00
Damien Elmes
554081cee3 convert svelte worker to ts
Should make it easier to maintain, and ironically it also fixes
the issue with .mjs files from this morning.
2021-03-20 23:13:27 +10:00
Damien Elmes
0cc06ad03b switch Svelte compilation to worker model
based on changes from upstream rules_svelte

Their code was using run_node() instead of ctx.actions.run(), which
seems to create a new worker for every CPU core, instead of respecting
the standard limit of 4.
2021-03-20 22:52:35 +10:00
Damien Elmes
e60eb27184 update Svelte and fix Windows build
Svelte 3.25.0 and onwards bundle compiler.mjs, which seems to be
preferentially used over the .js file. Presumably this is only breaking
on Windows due to the lack of a sandbox. Resolve by explicitly requesting
the .js file.
2021-03-20 16:13:20 +10:00
Damien Elmes
ea7611d8cc vendor Svelte rules
The separate repo made it difficult to update the rules, and made things
more complicated than they needed to be.
2021-03-20 15:04:19 +10:00