Skip to content

Commit

Permalink
remove the use of expandos
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Jan 9, 2024
1 parent ae401b2 commit 1904e7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
42 changes: 19 additions & 23 deletions pkgs/sketch_pad/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:async';
import 'dart:js_interop';
import 'dart:math' as math;
Expand All @@ -17,14 +15,27 @@ import 'package:web/web.dart' as web;
import '../model.dart';
import 'codemirror.dart';

const String _viewType = 'dartpad-editor';

bool _viewFactoryInitialized = false;
CodeMirror? codeMirrorInstance;

final Key _elementViewKey = UniqueKey();

void _initViewFactory() {
if (_viewFactoryInitialized) return;
_viewFactoryInitialized = true;

ui_web.platformViewRegistry
.registerViewFactory(_viewType, _codeMirrorFactory);
}

web.Element _codeMirrorFactory(int viewId) {
final div = web_helpers.createElementTag('div') as web.HTMLDivElement
..style.width = '100%'
..style.height = '100%';

final codeMirror = CodeMirror(
codeMirrorInstance = CodeMirror(
div,
<String, dynamic>{
'lineNumbers': true,
Expand All @@ -35,31 +46,17 @@ web.Element _codeMirrorFactory(int viewId) {
}.jsify());

CodeMirror.commands.goLineLeft =
((JSObject? _) => _handleGoLineLeft(codeMirror)).toJS;
((JSObject? _) => _handleGoLineLeft(codeMirrorInstance!)).toJS;
CodeMirror.commands.indentIfMultiLineSelectionElseInsertSoftTab =
((JSObject? _) =>
_indentIfMultiLineSelectionElseInsertSoftTab(codeMirror)).toJS;
_indentIfMultiLineSelectionElseInsertSoftTab(codeMirrorInstance!))
.toJS;
CodeMirror.commands.weHandleElsewhere =
((JSObject? _) => _weHandleElsewhere(codeMirror)).toJS;

_expando[div] = codeMirror;
((JSObject? _) => _weHandleElsewhere(codeMirrorInstance!)).toJS;

return div;
}

const String _viewType = 'dartpad-editor';
final Expando _expando = Expando(_viewType);

bool _viewFactoryInitialized = false;

void _initViewFactory() {
if (_viewFactoryInitialized) return;
_viewFactoryInitialized = true;

ui_web.platformViewRegistry
.registerViewFactory(_viewType, _codeMirrorFactory);
}

class EditorWidget extends StatefulWidget {
final AppModel appModel;
final AppServices appServices;
Expand Down Expand Up @@ -120,8 +117,7 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
}

void _platformViewCreated(int id, {required bool darkMode}) {
final div = ui_web.platformViewRegistry.getViewById(id) as web.Element;
codeMirror = _expando[div] as CodeMirror;
codeMirror = codeMirrorInstance;

// read only
final readOnly = !widget.appModel.appReady.value;
Expand Down
17 changes: 5 additions & 12 deletions pkgs/sketch_pad/lib/execution/execution.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:ui_web' as ui_web;

import 'package:flutter/material.dart';
Expand All @@ -14,12 +12,12 @@ import '../model.dart';
import '../theme.dart';
import 'frame.dart';

final Key _elementViewKey = UniqueKey();

const String _viewType = 'dartpad-execution';
final Expando _expando = Expando(_viewType);

bool _viewFactoryInitialized = false;
ExecutionService? executionServiceInstance;

final Key _elementViewKey = UniqueKey();

void _initViewFactory() {
if (_viewFactoryInitialized) return;
Expand All @@ -39,9 +37,7 @@ web.Element _iFrameFactory(int viewId) {
..style.width = '100%'
..style.height = '100%';

final executionService = ExecutionServiceImpl(frame);

_expando[frame] = executionService;
executionServiceInstance = ExecutionServiceImpl(frame);

return frame;
}
Expand Down Expand Up @@ -78,10 +74,7 @@ class _ExecutionWidgetState extends State<ExecutionWidget> {
key: _elementViewKey,
viewType: _viewType,
onPlatformViewCreated: (int id) {
final frame =
ui_web.platformViewRegistry.getViewById(id) as web.Element;
final executionService = _expando[frame] as ExecutionService;
widget.appServices.registerExecutionService(executionService);
widget.appServices.registerExecutionService(executionServiceInstance);
},
),
);
Expand Down

0 comments on commit 1904e7e

Please sign in to comment.