Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial checkin for Image within Text nodes
Summary: This adds the basic support for embedding an image in a TextView. Implementation details : We create a ReactTextInlineImageShadowNode whenever an Image is embedded within a Text context. That uses the same parsing code as ReactImageView (copied, not shared) to parse the source property to figure out the Uri where the resource is. In ReactTextShadowNode we now look for the ReactTextInlineImageShadowNode and place a TextInlineImageSpan so that we can layout appropriately Later at the time we go to setText on the TextView, we update that TextInlineImageSpan so that the proper Drawable (downloaded via Fresco) can be shown public Reviewed By: mkonicek Differential Revision: D2652667 fb-gh-sync-id: 8f24924d204f78b8bc4d5d67835cc73b3c1859dd
- Loading branch information
Dave Miller
authored and
facebook-github-bot-0
committed
Nov 13, 2015
1 parent
492412f
commit a0268a7
Showing
13 changed files
with
544 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextInlineImageShadowNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.text; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Locale; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
|
||
import com.facebook.common.util.UriUtil; | ||
import com.facebook.react.uimanager.LayoutShadowNode; | ||
import com.facebook.react.uimanager.ReactProp; | ||
import com.facebook.react.uimanager.ReactShadowNode; | ||
|
||
/** | ||
* {@link ReactShadowNode} class for Image embedded within a TextView. | ||
* | ||
*/ | ||
public class ReactTextInlineImageShadowNode extends LayoutShadowNode { | ||
|
||
private @Nullable Uri mUri; | ||
|
||
@ReactProp(name = "src") | ||
public void setSource(@Nullable String source) { | ||
Uri uri = null; | ||
if (source != null) { | ||
try { | ||
uri = Uri.parse(source); | ||
// Verify scheme is set, so that relative uri (used by static resources) are not handled. | ||
if (uri.getScheme() == null) { | ||
uri = null; | ||
} | ||
} catch (Exception e) { | ||
// ignore malformed uri, then attempt to extract resource ID. | ||
} | ||
if (uri == null) { | ||
uri = getResourceDrawableUri(getThemedContext(), source); | ||
} | ||
} | ||
if (uri != mUri) { | ||
markUpdated(); | ||
} | ||
mUri = uri; | ||
} | ||
|
||
public @Nullable Uri getUri() { | ||
return mUri; | ||
} | ||
|
||
// TODO: t9053573 is tracking that this code should be shared | ||
private static @Nullable Uri getResourceDrawableUri(Context context, @Nullable String name) { | ||
if (name == null || name.isEmpty()) { | ||
return null; | ||
} | ||
name = name.toLowerCase(Locale.getDefault()).replace("-", "_"); | ||
int resId = context.getResources().getIdentifier( | ||
name, | ||
"drawable", | ||
context.getPackageName()); | ||
return new Uri.Builder() | ||
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME) | ||
.path(String.valueOf(resId)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public boolean isVirtual() { | ||
return true; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...tAndroid/src/main/java/com/facebook/react/views/text/ReactTextInlineImageViewManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.text; | ||
|
||
import android.view.View; | ||
|
||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.ViewManager; | ||
|
||
/** | ||
* Manages Images embedded in Text nodes. Since they are used only as a virtual nodes any type of | ||
* native view operation will throw an {@link IllegalStateException} | ||
*/ | ||
public class ReactTextInlineImageViewManager | ||
extends ViewManager<View, ReactTextInlineImageShadowNode> { | ||
|
||
static final String REACT_CLASS = "RCTTextInlineImage"; | ||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
} | ||
|
||
@Override | ||
public View createViewInstance(ThemedReactContext context) { | ||
throw new IllegalStateException("RCTTextInlineImage doesn't map into a native view"); | ||
} | ||
|
||
@Override | ||
public ReactTextInlineImageShadowNode createShadowNodeInstance() { | ||
return new ReactTextInlineImageShadowNode(); | ||
} | ||
|
||
@Override | ||
public Class<ReactTextInlineImageShadowNode> getShadowNodeClass() { | ||
return ReactTextInlineImageShadowNode.class; | ||
} | ||
|
||
@Override | ||
public void updateExtraData(View root, Object extraData) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.