Skip to content

Commit e642dfa

Browse files
committed
Resolved #12 - Code Mirror for better editing, Code mirror class
1 parent 31e070f commit e642dfa

9 files changed

Lines changed: 146 additions & 32 deletions

File tree

dart/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ publish_to: none
66
environment:
77
sdk: 3.6.2
88

9-
# Add regular dependencies here.
109
dependencies:
1110
http: ^1.3.0
1211
wallpaper_generator:
@@ -15,6 +14,8 @@ dependencies:
1514
path: ../packages/wallpaper
1615
wallpaper_worker:
1716
path: ../packages/wallpaper_worker
17+
code_mirror:
18+
path: ../packages/code_mirror
1819

1920
dev_dependencies:
2021
build_runner: ^2.4.15

dart/web/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</div>
2929

3030
<div class="container">
31-
<textarea id="input" cols="50" rows="22" spellcheck="false"></textarea>
31+
<div class="codemirror-container" id="input"></div>
3232
<div class="handle" id="resize-handle"></div>
3333
<div class="canvas-container">
3434
<div id="loading-panel" class="loading-panel">
@@ -62,6 +62,14 @@
6262
canvasContainer.style.flex = `1`;
6363
}
6464
</script>
65+
66+
<!--Startup Code Mirror 5-->
67+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.css">
68+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/theme/material-darker.min.css">
69+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.js"></script>
70+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/javascript/javascript.min.js"></script>
71+
72+
<!--Application Startup-->
6573
<script src="main.js" type="text/javascript"></script>
6674
</body>
6775

dart/web/main.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@ import 'dart:math';
33
import 'dart:html';
44
import 'dart:async';
55

6+
import 'package:code_mirror/code_mirror.dart';
67
import 'package:wallpaper/wallpaper.dart';
78

89
final CanvasElement canvas = querySelector("#output") as CanvasElement;
910
final OffscreenCanvas offscreenCanvas = canvas.transferControlToOffscreen();
1011
final OffscreenCanvasRenderingContext2D offCtx = offscreenCanvas.getContext('2d') as OffscreenCanvasRenderingContext2D;
1112

12-
final TextAreaElement textArea = document.querySelector("#input") as TextAreaElement;
1313
final ButtonElement saveBtn = document.querySelector("#save-btn") as ButtonElement;
1414
final DivElement loading = document.querySelector("#loading-panel") as DivElement;
1515
final ParagraphElement loadingMessage = document.querySelector("#loading-message") as ParagraphElement;
1616

17+
final CodeMirrorEditor codeEditor = CodeMirrorEditor("#input");
18+
1719
const int numberOfConfigurations = 7;
1820
Timer? _debounceTimer;
1921

2022
final String workerName = "wallpaper_generator_worker.js";
2123
Worker? worker;
2224

2325
void main() async {
24-
2526
// Set first wallpaper
2627
await setFirstWallpaper();
2728

@@ -36,8 +37,8 @@ Future setFirstWallpaper() async {
3637
}
3738

3839
void bindUI() {
39-
textArea.onInput.listen((_) => debounceUpdateWallpaper());
4040
saveBtn.onClick.listen((_) => saveImage());
41+
codeEditor.onChange(debounceUpdateWallpaper);
4142
}
4243

4344
void saveImage() {
@@ -58,26 +59,28 @@ void debounceUpdateWallpaper() {
5859

5960
void updateWallpaper() {
6061
print("Wallpaper Update");
61-
String compactJson = getJsonFromTextArea();
62+
String? compactJson = codeEditor.getValue();
63+
if (compactJson == null) return;
6264
Wallpaper? wallpaper = Wallpaper.fromRawJson(compactJson);
63-
setWallpaper(wallpaper);
65+
updateCanvas(wallpaper);
6466
}
6567

6668
void setWallpaper(Wallpaper? wallpaper) {
6769
if (wallpaper == null) return;
68-
setJsonInTextArea(wallpaper.toRawJson());
70+
setJsonInCodeMirror(wallpaper.toRawJson());
6971
updateCanvas(wallpaper);
7072
}
7173

72-
void setJsonInTextArea(String? jsonString) {
74+
void setJsonInCodeMirror(String? jsonString) {
7375
if (jsonString == null) return;
7476
Map<String, dynamic>? map = json.decoder.convert(jsonString);
7577
if (map == null) return;
7678
JsonEncoder encoder = JsonEncoder.withIndent(' ');
7779
String prettyprint = encoder.convert(map);
78-
textArea.text = prettyprint;
80+
codeEditor.setValue(prettyprint);
7981
}
8082

83+
8184
// Stop the current worker and start another one
8285
void updateCanvas(Wallpaper? wallpaper) {
8386
if (wallpaper == null) return;
@@ -104,12 +107,6 @@ void updateCanvas(Wallpaper? wallpaper) {
104107
worker?.postMessage({'wallpaper' : wallpaper.toJson()});
105108
}
106109

107-
String getJsonFromTextArea() {
108-
String jsonString = textArea.value!.trim();
109-
Map<String, dynamic> jsonData = jsonDecode(jsonString);
110-
return jsonEncode(jsonData);
111-
}
112-
113110
Uri getRandomInitialConfiguration() {
114111
int index = Random.secure().nextInt(numberOfConfigurations) + 1;
115112
return Uri.parse("https://lucaffo.github.io/github-wallpapers/static/wallpapers/wallpaper_${index.toString().padLeft(2, '0')}.json");

dart/web/styles/styles.css

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,25 @@ body {
1414
height: 100vh;
1515
}
1616

17-
.container textarea {
18-
flex: 1;
19-
resize: none;
20-
border: none;
21-
padding: 10px;
17+
.codemirror-container {
18+
display: flex;
19+
justify-content: center;
20+
align-items: center;
21+
height: 100vh;
22+
background-color: #1e1e1e;
23+
padding: 0;
2224
margin: 0;
23-
font-size: 20px;
24-
align-content: center;
25-
background-color: #333;
26-
color: #fff;
27-
outline: none;
28-
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
25+
}
26+
27+
.codemirror-container > div {
28+
width: 100%;
29+
height: 100%;
30+
font-size: 18px;
31+
padding-right: 20px;
32+
}
33+
34+
.CodeMirror-vscrollbar {
35+
display: none;
2936
}
3037

3138
.handle {
@@ -55,17 +62,17 @@ body {
5562
}
5663

5764
.home-button-container {
58-
position: absolute;
59-
top: 10px;
60-
left: 10px;
6165
display: flex;
66+
position: relative;
6267
gap: 10px;
68+
padding: 8px;
69+
background-color: #333;
6370
}
6471

6572
.save-button-container {
6673
position: absolute;
67-
top: 10px;
68-
right: 10px;
74+
top: 8px;
75+
right: 8px;
6976
display: flex;
7077
gap: 10px;
7178
}

packages/code_mirror/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
5+
# Avoid committing pubspec.lock for library packages; see
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
7+
pubspec.lock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library;
2+
3+
export 'src/code_mirror_editor.dart';
4+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'dart:js';
2+
import 'dart:html';
3+
4+
class CodeMirrorEditor {
5+
late JsObject _instance;
6+
7+
bool _avoidChange = false;
8+
9+
/// Default CodeMirror options
10+
static final Map<String, dynamic> _defaultOptions = {
11+
'mode': 'javascript',
12+
'theme': 'material-darker',
13+
'lineNumbers': true,
14+
'indentWithTabs' : true,
15+
'matchBrackets' : true
16+
};
17+
18+
CodeMirrorEditor(String elementId) {
19+
var element = document.querySelector(elementId);
20+
if (element == null) {
21+
throw ArgumentError("Element cannot be found at ID '$elementId'");
22+
}
23+
24+
_instance = JsObject(context['CodeMirror'], [element, JsObject.jsify(_defaultOptions)]);
25+
}
26+
27+
/// Listen for content changes.
28+
void onChange(Function callback) {
29+
final onChangeCallback = JsFunction.withThis((window, arg1, arg2) {
30+
if (_avoidChange) {_avoidChange = false; return; }
31+
callback();
32+
});
33+
34+
_instance.callMethod('on', ['change', onChangeCallback]);
35+
}
36+
37+
/// Set the content of the editor.
38+
void setValue(String? content) {
39+
_avoidChange = true;
40+
_instance.callMethod('setValue', [content]);
41+
}
42+
43+
/// Get the current content of the editor.
44+
String? getValue() {
45+
return _instance.callMethod('getValue', []);
46+
}
47+
}

packages/code_mirror/pubspec.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: code_mirror
2+
description: A code mirror interop package
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ^3.6.2
8+
9+
dependencies:
10+
11+
dev_dependencies:
12+
lints: ^5.0.0
13+
test: ^1.24.0

0 commit comments

Comments
 (0)