Skip to content

Commit aa2cdde

Browse files
committed
Added site preview
1 parent 268cb09 commit aa2cdde

11 files changed

Lines changed: 289 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ capability.
2727

2828
## Features
2929

30-
- [ ] Realtime website preview
30+
- [x] Realtime website preview
3131
- [ ] Git manager
3232
- [ ] DevTools
3333
- [ ] Parse HTML layout from page

main/src/main/AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,18 @@
2929
<activity
3030
android:name=".EditorActivity"
3131
android:exported="false" />
32+
<activity
33+
android:name=".PreviewActivity"
34+
android:exported="true">
35+
<intent-filter>
36+
<action android:name="android.intent.action.VIEW" />
37+
<category android:name="android.intent.category.DEFAULT" />
38+
<category android:name="android.intent.category.BROWSABLE" />
39+
<data android:scheme="file" />
40+
<data android:scheme="content" />
41+
<data android:mimeType="text/html" />
42+
<data android:pathPattern=".*\\.html" />
43+
</intent-filter>
44+
</activity>
3245
</application>
3346
</manifest>

main/src/main/java/org/xedox/webaide/EditorActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ public void handleBackPressed() {
7575

7676
@Override
7777
public boolean onOptionsItemSelected(MenuItem item) {
78-
return editorManager.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
78+
if(editorManager.onOptionsItemSelected(item)) return true;
79+
if(item.getItemId() == R.id.run) {
80+
Intent intent = new Intent(this, PreviewActivity.class);
81+
intent.putExtra("index.html", project.file("index.html"));
82+
startActivity(intent);
83+
}
84+
return super.onOptionsItemSelected(item);
7985
}
8086

8187

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.xedox.webaide;
2+
3+
import android.content.Intent;
4+
import android.graphics.Bitmap;
5+
import android.net.Uri;
6+
import android.os.Bundle;
7+
import android.view.MenuItem;
8+
import android.view.View;
9+
import android.webkit.WebChromeClient;
10+
import android.webkit.WebResourceRequest;
11+
import android.webkit.WebView;
12+
import android.webkit.WebViewClient;
13+
import android.widget.ProgressBar;
14+
import androidx.appcompat.app.AppCompatActivity;
15+
import com.google.android.material.appbar.MaterialToolbar;
16+
import java.io.BufferedReader;
17+
import java.io.InputStream;
18+
import java.io.InputStreamReader;
19+
import org.xedox.utils.BaseActivity;
20+
import org.xedox.utils.view.WebViewX;
21+
22+
public class PreviewActivity extends BaseActivity {
23+
24+
private MaterialToolbar toolbar;
25+
private WebViewX webView;
26+
private ProgressBar progress;
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
setContentView(R.layout.activity_preview);
32+
toolbar = findViewById(R.id.toolbar);
33+
webView = findViewById(R.id.web_view);
34+
progress = findViewById(R.id.progress);
35+
setSupportActionBar(toolbar);
36+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
37+
webView.setProgressBar(progress);
38+
webView.addOnPageStartedListener((v, url, ic)-> {
39+
getSupportActionBar().setSubtitle(url);
40+
});
41+
42+
Intent intent = getIntent();
43+
String indexHtml = intent.getStringExtra("index.html");
44+
45+
if (indexHtml != null && !indexHtml.isBlank()) {
46+
webView.loadUrl("file://"+indexHtml);
47+
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
48+
Uri data = intent.getData();
49+
if (data != null) {
50+
loadHtmlFromUri(data);
51+
} else {
52+
webView.loadData("File found'n", "text/html", "UTF-8");
53+
}
54+
}
55+
}
56+
57+
@Override
58+
public boolean onOptionsItemSelected(MenuItem item) {
59+
int id = item.getItemId();
60+
if(id == android.R.id.home) {
61+
finish();
62+
}
63+
return super.onOptionsItemSelected(item);
64+
}
65+
66+
67+
@Override
68+
public void handleBackPressed() {
69+
finish();
70+
}
71+
72+
private void loadHtmlFromUri(Uri uri) {
73+
try (InputStream is = getContentResolver().openInputStream(uri);
74+
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
75+
StringBuilder buffer = new StringBuilder();
76+
String line;
77+
while ((line = br.readLine()) != null) {
78+
buffer.append(line).append("\n");
79+
}
80+
webView.loadData(buffer.toString(), "text/html", "UTF-8");
81+
} catch (Exception e) {
82+
webView.loadData("Ошибка загрузки: " + e.getMessage(), "text/html", "UTF-8");
83+
}
84+
}
85+
}

main/src/main/java/org/xedox/webaide/project/Project.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public static Project createProject(Context context, String name) throws IOExcep
8484
}
8585
return new Project(projectDir);
8686
}
87+
88+
public String file(String path) {
89+
return new File(projectPath, path).getAbsolutePath();
90+
}
8791

8892
public static void renameProject(Project project, String newName) {
8993
File newFile = new File(project.getParent(), newName);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
5+
<gradient
6+
android:startColor="#FF00FF"
7+
android:centerColor="#FF0000"
8+
android:endColor="#FFFF00"
9+
android:angle="0"
10+
android:type="linear"/>
11+
12+
<corners android:radius="16dp" />
13+
14+
</shape>

main/src/main/res/drawable/run.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- drawable/play.xml -->
2+
<vector
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:height="24dp"
5+
android:width="24dp"
6+
android:viewportWidth="24"
7+
android:viewportHeight="24">
8+
<path
9+
android:fillColor="@drawable/icon_tint"
10+
android:pathData="M8,5.14V19.14L19,12.14L8,5.14Z" />
11+
</vector>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_height="match_parent"
6+
android:layout_width="match_parent">
7+
8+
<com.google.android.material.appbar.MaterialToolbar
9+
android:layout_height="?attr/actionBarSize"
10+
android:layout_width="match_parent"
11+
app:title="@string/app_name"
12+
android:id="@+id/toolbar" />
13+
14+
<ProgressBar
15+
android:id="@+id/progress"
16+
android:layout_height="5dp"
17+
android:layout_width="match_parent"
18+
android:layout_below="@+id/toolbar"
19+
style="?android:attr/progressBarStyleHorizontal"
20+
android:progressDrawable="@drawable/progress" />
21+
22+
<org.xedox.utils.view.WebViewX
23+
android:id="@+id/web_view"
24+
android:layout_height="match_parent"
25+
android:layout_width="match_parent"
26+
android:layout_below="@+id/progress" />
27+
28+
</RelativeLayout>

main/src/main/res/menu/editor.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
2+
<menu
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto">
35

46
<item
57
android:id="@+id/save_all"
68
android:title="@+id/save_all" />
79

10+
<item
11+
android:id="@+id/run"
12+
android:title="@+id/run"
13+
android:icon="@drawable/run"
14+
app:showAsAction="always"/>
15+
816
</menu>

main/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<string name="file_tree">File tree</string>
3434
<string name="settings">Settings</string>
3535
<string name="exit">Exit</string>
36+
<string name="run">Run</string>
3637
<string name="close_it">Close this</string>
3738
<string name="save_all">Save all</string>
3839
<string name="close_other">Close others</string>

0 commit comments

Comments
 (0)