Skip to content

Commit a77b382

Browse files
authored
feat: support hash router. (#572)
Support hash router and compatible with Vue-Router with WebHashHistory(). 1. click a `<a>` element with a path starts with '#' will trigger an `hashupdate` event instead of refresh. 2. Add `hashchange` Event support.
2 parents b69a337 + 088013c commit a77b382

File tree

15 files changed

+266
-10
lines changed

15 files changed

+266
-10
lines changed

bridge/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
340340
core/events/ui_event.cc
341341
core/events/focus_event.cc
342342
core/events/gesture_event.cc
343+
core/events/hashchange_event.cc
343344
core/events/input_event.cc
344345
core/events/touch_event.cc
345346
core/events/mouse_event.cc
@@ -427,6 +428,8 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
427428
out/qjs_ui_event.cc
428429
out/qjs_ui_event_init.cc
429430
out/qjs_gesture_event.cc
431+
out/qjs_hashchange_event.cc
432+
out/qjs_hashchange_event_init.cc
430433
out/qjs_gesture_event_init.cc
431434
out/qjs_intersection_change_event.cc
432435
out/qjs_intersection_change_event_init.cc

bridge/bindings/qjs/binding_initializer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qjs_event_target.h"
3434
#include "qjs_focus_event.h"
3535
#include "qjs_gesture_event.h"
36+
#include "qjs_hashchange_event.h"
3637
#include "qjs_html_all_collection.h"
3738
#include "qjs_html_anchor_element.h"
3839
#include "qjs_html_body_element.h"
@@ -118,6 +119,7 @@ void InstallBindings(ExecutingContext* context) {
118119
QJSCloseEvent::Install(context);
119120
QJSFocusEvent::Install(context);
120121
QJSGestureEvent::Install(context);
122+
QJSHashchangeEvent::Install(context);
121123
QJSInputEvent::Install(context);
122124
QJSCustomEvent::Install(context);
123125
QJSMouseEvent::Install(context);

bridge/bindings/qjs/wrapper_type_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum {
3232
JS_CLASS_ANIMATION_EVENT,
3333
JS_CLASS_FOCUS_EVENT,
3434
JS_CLASS_GESTURE_EVENT,
35+
JS_CLASS_HASHCHANGE_EVENT,
3536
JS_CLASS_POP_STATE_EVENT,
3637
JS_CLASS_INTERSECTION_CHANGE_EVENT,
3738
JS_CLASS_KEYBOARD_EVENT,

bridge/core/dom/events/event.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ bool Event::IsPopstateEvent() const {
302302
return false;
303303
}
304304

305+
bool Event::IsHashChangeEvent() const {
306+
return false;
307+
}
308+
305309
bool Event::IsIntersectionchangeEvent() const {
306310
return false;
307311
}

bridge/core/dom/events/event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class Event : public ScriptWrappable {
171171
virtual bool IsMessageEvent() const;
172172
virtual bool IsPopstateEvent() const;
173173
virtual bool IsIntersectionchangeEvent() const;
174+
virtual bool IsHashChangeEvent() const;
174175

175176
// Drag events are a subset of mouse events.
176177
virtual bool IsDragEvent() const;

bridge/core/events/dart_created_events.json5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"gestureend"
3838
]
3939
},
40+
"hashchange",
4041
"input",
4142
{
4243
"class": "FocusEvent",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2022-present The WebF authors. All rights reserved.
3+
*/
4+
5+
#include "hashchange_event.h"
6+
#include "qjs_hashchange_event.h"
7+
8+
namespace webf {
9+
10+
HashchangeEvent* HashchangeEvent::Create(webf::ExecutingContext* context,
11+
const webf::AtomicString& type,
12+
webf::ExceptionState& exception_state) {
13+
return MakeGarbageCollected<HashchangeEvent>(context, type, exception_state);
14+
}
15+
16+
HashchangeEvent* HashchangeEvent::Create(webf::ExecutingContext* context,
17+
const webf::AtomicString& type,
18+
const std::shared_ptr<HashchangeEventInit>& initializer,
19+
webf::ExceptionState& exception_state) {
20+
return MakeGarbageCollected<HashchangeEvent>(context, type, initializer, exception_state);
21+
}
22+
23+
HashchangeEvent::HashchangeEvent(webf::ExecutingContext* context,
24+
const webf::AtomicString& type,
25+
webf::ExceptionState& exception_state)
26+
: Event(context, type) {}
27+
28+
HashchangeEvent::HashchangeEvent(webf::ExecutingContext* context,
29+
const webf::AtomicString& type,
30+
const std::shared_ptr<HashchangeEventInit>& initializer,
31+
webf::ExceptionState& exception_state)
32+
: Event(context, type),
33+
new_url_(initializer->hasNewURL() ? initializer->newURL() : AtomicString::Empty()),
34+
old_url_(initializer->hasOldURL() ? initializer->oldURL() : AtomicString::Empty()) {}
35+
36+
HashchangeEvent::HashchangeEvent(webf::ExecutingContext* context,
37+
const webf::AtomicString& type,
38+
webf::NativeHashchangeEvent* native_hash_change_event)
39+
: Event(context, type, &native_hash_change_event->native_event),
40+
#if ANDROID_32_BIT
41+
new_url_(AtomicString(ctx(),
42+
std::unique_ptr<AutoFreeNativeString>(
43+
reinterpret_cast<AutoFreeNativeString*>(native_hash_change_event->newURL)))),
44+
old_url_(AtomicString(ctx(),
45+
std::unique_ptr<AutoFreeNativeString>(
46+
reinterpret_cast<AutoFreeNativeString*>(native_hash_change_event->oldURL))))
47+
#else
48+
new_url_(AtomicString(ctx(),
49+
std::unique_ptr<AutoFreeNativeString>(
50+
reinterpret_cast<AutoFreeNativeString*>(native_hash_change_event->newURL)))),
51+
old_url_(AtomicString(ctx(),
52+
std::unique_ptr<AutoFreeNativeString>(
53+
reinterpret_cast<AutoFreeNativeString*>(native_hash_change_event->oldURL))))
54+
#endif
55+
{
56+
}
57+
58+
bool HashchangeEvent::IsHashChangeEvent() const {
59+
return true;
60+
}
61+
62+
const AtomicString& HashchangeEvent::newURL() const {
63+
return new_url_;
64+
}
65+
66+
const AtomicString& HashchangeEvent::oldURL() const {
67+
return old_url_;
68+
}
69+
70+
} // namespace webf
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Event} from "../dom/events/event";
2+
import {GestureEventInit} from "./gesture_event_init";
3+
import {HashchangeEventInit} from "./hashchange_event_init";
4+
5+
interface HashchangeEvent extends Event {
6+
readonly newURL: string;
7+
readonly oldURL: string;
8+
[key: string]: any;
9+
new(type: string, init?: HashchangeEventInit): HashchangeEvent;
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2022-present The WebF authors. All rights reserved.
3+
*/
4+
5+
#ifndef WEBF_CORE_EVENTS_HASHCHANGE_EVENT_H_
6+
#define WEBF_CORE_EVENTS_HASHCHANGE_EVENT_H_
7+
8+
#include "bindings/qjs/dictionary_base.h"
9+
#include "bindings/qjs/source_location.h"
10+
#include "core/dom/events/event.h"
11+
#include "qjs_hashchange_event_init.h"
12+
13+
namespace webf {
14+
15+
struct NativeHashchangeEvent;
16+
17+
class HashchangeEvent : public Event {
18+
DEFINE_WRAPPERTYPEINFO();
19+
20+
public:
21+
using ImplType = HashchangeEvent*;
22+
23+
static HashchangeEvent* Create(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state);
24+
25+
static HashchangeEvent* Create(ExecutingContext* context,
26+
const AtomicString& type,
27+
const std::shared_ptr<HashchangeEventInit>& initializer,
28+
ExceptionState& exception_state);
29+
30+
explicit HashchangeEvent(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state);
31+
32+
explicit HashchangeEvent(ExecutingContext* context,
33+
const AtomicString& type,
34+
const std::shared_ptr<HashchangeEventInit>& initializer,
35+
ExceptionState& exception_state);
36+
37+
explicit HashchangeEvent(ExecutingContext* context,
38+
const AtomicString& type,
39+
NativeHashchangeEvent* native_hash_change_event);
40+
41+
const AtomicString& newURL() const;
42+
const AtomicString& oldURL() const;
43+
44+
bool IsHashChangeEvent() const override;
45+
46+
private:
47+
AtomicString new_url_;
48+
AtomicString old_url_;
49+
};
50+
51+
} // namespace webf
52+
53+
#endif // WEBF_CORE_EVENTS_HASHCHANGE_EVENT_H_
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { EventInit } from "../dom/events/event_init";
2+
3+
// @ts-ignore
4+
@Dictionary()
5+
export interface HashchangeEventInit extends EventInit {
6+
oldURL?: string;
7+
newURL?: string;
8+
}

0 commit comments

Comments
 (0)